npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nodutils

v0.1.5

Published

Node utilities to manage common operations over: string, numeric, array, date, file, url, cache, properties files, geocoding

Downloads

5

Readme

Utilities for NodeJS

Installation

npm install nodutils

Usage

var utils = require("nodutils");

String

  • utils.string.trim(str) or String.trim()

  • utils.string.ltrim(str) or String.trim()

  • utils.string.rtrim(str) or String.trim()

  • utils.string.toi(str) or String.toi()

    • Converts to integer
  • utils.string.tof(str[,decimals]) or String.tof([decimals])

    • Converts to float, with number of decimals
  • utils.string.dropDiacritics(str) or String.dropDiacritics()

    • Converts accents, diacritics into a plain letter
  • utils.string.isNumber(str) or String.isNumber()

  • utils.string.stripHtml(str) or String.stripHtml()

    • Strip all html tags and leaves only text
  • utils.string.contains(str,substr[,flags]) or String.contains(substr[,flags]).

    • It counts the number of ocurrences of substr. Flags can be "i" (ignore case) and/or "d" (drop accents)
  • utils.string.reverse(str) or String.reverse()

  • utils.string.toHtml(str) or String.toHtml()

    • Converts diacritics and almost all chars into html entities
  • utils.string.fromHtml(str) or String.fromHtml()

    • Converts into diacritics html encoded entities

Numeric

  • utils.number.round(num[,decimals]) or Number.round([decimals])

    • Rounds number to the given number of decimals

Array

  • utils.array.max(array) or Array.max()

    • Returns the max value in an array of numbers
  • utils.array.min(array) or Array.min()

    • Returns the min value in an array of numbers
  • utils.array.uniques(array) or Array.uniques()

    • Returns an array of uniques values in the original array
  • utils.array.aggregate(array) or Array.aggregate()

    • Returns an array of uniques values and counts its occurrences, sorted descending (sample code)

      	//returned array
      	[['itemX',15],['itemY',12],....]
  • utils.array.contains(array,value[,flags]) or Array.contains(value[,flags])

    • Returns the number of occurrences of "value"

    • Flags can be "i" (ignore case) and/or "d" (drop accents)

Date

  • utils.date.diff(date1,date2[,unit])

    • Unit = "d": days,"h": hours,"m": minutes,"s": seconds. Default unit is millis
  • utils.date.millis([date])

    • Returns a timestamp in millis from current date or date passed as param (string or date object)
  • utils.date.frommillis(millis)

    • Returns a date object from millis passed as parameter

File

  • utils.file.write(file,data[,options],callback)

    • options = "w" write or "a" append
  • utils.file.read(file[,encoding],callback)

  • utils.file.exists(file,callback)

  • utils.file.getModTime(file,callback)

    • Date object is given to the callback as an argument
  • utils.file.remove(file,callback)

  • utils.file.createpath(path,callback)

Url

			//get sample
			utils.url.get("www.bbc.com",function(html){
				console.log(html);
			});			

			//post sample
			utils.url.post('httpbin.org/post',{post_data:{data:'lorem ipsum dolor sit amet'}}, 
				function(resp){
					console.log(resp);
				}
			);
			
  • utils.url.get(url[,options],callback)

  • utils.url.post(url[,options],callback)

    • Support for http and https

    • Support for proxy requests (in url. E.g: "url"=http://www.proxy.com:8080/www.urltobeproxied.com)

    • It is possible to set only url or options, but options need to set host, path, ...

    • Options is an object with some props:

      • "encoding" default is "utf-8"

      • "post_data" (for post()) is an object

        with the vars (post_data:{a:1,b:2})

        or with the body in "data" key (post_data:{data:"whatever"})

      • "headers" (object)

      • "auth" (string)

      • "forceparse": if the url is with proxy data is better to set to true

Cache

  • utils.cache.getPath()

    • Get the current cache dir (default is "./cache")
  • utils.cache.setPath(path,callback)

    • Set the cache dir (and create if it doesn't exists)

    • It's recommended to use absolute paths ("/apps/myapp/cache")

  • utils.cache.setOptions({path : "/mypath", size : 1}},callback)

    • Set the cache dir (and create if it doesn't exists) and cache max size

    • Cache size is in MB

  • utils.cache.set(key,data[,expiretime],callback)

    • Expiretime is in seconds. If not informed, then unlimited
  • utils.cache.get(key,callback)

  • utils.cache.remove(key,callback)

(sample code)

Properties

  • utils.props.load(path_to_file,callback)

    • properties in the file are loaded into a JSON object passed in callback

    • Format of the properties file is

        	key1=value1
        	key2=value2
        	key3=value3
        	key4=123456789
  • utils.props.save(path_to_file,properties,callback)

    • properties param is a json object. You can save properties dynamically

Samples

  • Caching twitter request due to twitter api limits (it uses url and cache utilities)

    		var utils = require("nodutils");
    		var twitterquery = "davidayalas";
    		var twitterurl = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=";
    
    		utils.cache.get(twitterquery, function(content){
    			if(!content){
    				utils.url.get(twitterurl+twitterquery,function(result){
    					utils.cache.set(twitterquery,result,300);
    					console.log(result);
    				});
    			}else{
    				console.log(content);
    			}
    		});
  • Easy "tagcloud" from url content (it uses url, string and array utilities)

      		var utils = require("nodutils");
    
      		utils.url.get("www.bbc.com",function(content){
      			var topwords = content.stripHtml().split(" ").aggregate().filter(function(i){
      				return i[0].length<=3 || i[0].indexOf("&")>-1?false:true;
      			}).slice(0,50);
      			console.log(topwords)
      		});