js.utils

General JavaScript Utilities

npm install js.utils
48 downloads in the last week
89 downloads in the last month

js.utils

Utilities for:

  • Object

    • copy/clone an object
    • contains - Validates if an object contains a given value
    • empty - Validates is the object empty
  • NPM

    • info - Get information about the list of installed packages
    • installed - Check if a package has already been installed
  • Template

    • Load and compile a template with underscore
  • Task

    • Lint, concat and/or minify a set of resources (currently JS file type supported)

Usage

Initial settings

  • Set the log on/off (by default set to on)
    require("js.utils").init({log: false});
    

Object

  • Copy the 'srcobj' to the 'destobj'

    var destobj = {},
    srcobj = {foo: 'foo'};
    require("js.utils").Object.copy(srcobj, destobj);
    
  • Validates if 'foo' exists in a given array

    require("js.utils").Object.contains(['foo'], 'foo');
    
  • Validates if an object is empty

    require("js.utils").Object.empty({});
    

NPM

  • get all local packages information

    require("js.utils").NPM.info({details: true}, function(err) {
             var data = this.data;
             // go over the array data
             data.forEach(function(item) {
                 if (item) {
                     console.log(" details: ",
                          JSON.stringify(item.get("details")));
                 }
             });
         });
    
  • check if the package 'bower' was installed

    require("js.utils").NPM.installed({list: ["bower"]},
          function(err) {
              console.log("Bower was: ",
                  (this.data.bower ? "" : "not"),
                  " installed");
          });
    

Template

  • Compile a given template as a file or string value using underscore (Mustache Style).
    var out = require("js.utils").Template.template({
             content: "Custom content Test was loaded {{status}}.",
             name: "templateTest",
             data: {
                 status: "successfully"
             }
         });
    

Task

  • Prepare your resources for Development (dev api) and/or Production (prod api) with
    JSHint & UglifyJS

    
          require("js.utils").Task.dev([{
                 src: ["./test/resources/test1.js","./test/resources/test2.js" ],
                 out: {
                     banner: "This is a test minified content",
                     name: "testx-min.js",
                     path: "./test/out"
                 }
             }, {
                 src: ["./test/resources/test1.js","./test/resources/test2.js" ],
                 out: {
                     banner: "This is a test minified content",
                     name: "testy-min.js",
                     path: "./test/out"
                 }
             }]);
    
  • Or use separate API minify/jshint/concat

    
          require("js.utils").Task.jshint({
                  src: ["./test/resources/test1.js"]
              });
    
          require("js.utils").Task.minify({
                  src: ["./test/resources/test1.js","./test/resources/test2.js" ]
              })
    

Versions

0.1.8

Task Module - Minify writer fix, for prod task

0.1.7

Object Module - 'resolve' method was added Task Module - file name added to the jshint's error output

0.1.6

Object Module - 'empty' method was added Task Module - expression path match added (e.g. ./*/.js)

0.1.5

Task module added to prepare your project for development / production, using JSHint & UglifyJS

0.1.4

XML module removed

0.1.3

XML module added with getting an XML/XSD document and validation functionality using the libxml package.

0.1.2

RequireJS dependency removed.

  • download the browser version
  • Usage

    • js.utils global variables for the web:
      • jsutilsObject;
      • jsutilsArray;
      • jsutilsTemplate;
  • Fixes for NPM package including:

    • JSON object sometimes failed to parse the npm list

0.0.9

Support for the browser version, including: Object, Array and Template(w/o template file loading support) download: jsutils-min.js

Assign a ready event:

jsutilsOnReady = function(obj, arr, tpl){
};

Or you can use js.utils global variables (when the page done loading):

  • jsutilsObject;
  • jsutilsArray;
  • jsutilsTemplate;

Reference

Object

Copy the source object's properties to the destination object.

  • copy(srcobj, destobj, override)
    • srcobj {Object} The source object
    • destobj {Object} The destination object
    • override {Object} Override the existing property (deault to false)

Check if a given object contains a value

  • contains(obj, value)
    • obj {Object} The referenced object
    • value {Object} The value to be searched

Is the object empty ?

  • empty(srcobj)
    • srcobj {Object} The object reference returns {boolean} If the object is [null || undefined || has no values {}] return true or else false

Inspect a given string and try to resolve its object

  • resolve(srcobj)
    • srcobj {Object} The object reference

NPM

Get information about the list of installed packages

  • info(config, callback)

    • config properties:
      • global {Boolean} the packages are global, optional values are true/false (defaults to false)
      • details {Boolean} Whether to display the full details or just the basic details. (defaults to false)
      • list {Array} specific list of packages names (or else result all)
      • depth {string} npm argument for how deep the rabbit hole goes (defaults to 10)
        • -1 for unlimited (Not recommended, NPM will might throw an error about "max depth reached")
    • callback {Function} callback function for collecting the results. (get the data: this.data)
      • err {String} In case an error occurred this variable will contain the error data or else 'undefined'
      • this.data {Object} Array object containing the information about the packages that was found.
                          Array item - instance of Package class
        
  • installed(config, callback)

    • config properties:
      • global {Boolean} the packages are global, optional values are true/false
      • list {Array} specific list of packages names (or else result all)
      • depth {string} npm argument for how deep the rabbit hole goes.
        • -1 for unlimited (Not recommended, NPM will might throw an error about "max depth reached")
    • callback {Function} callback function for collecting the results. (get the data: this.data)
      • err {String} In case an error occurred this variable will contain the error data or else 'undefined'
      • this.data {Object} An object containing the packages names with a true/false value indicating if the package installed.
  • Package class

    • getDetails Get the full details if the 'details' flag was set to true
    • getName Get the package name
    • get(key) Get the property by its key
      • opt keys 'name', 'dependencies', 'detail', etc.. (see package.json for more information)

Template

Load and compile template with underscore

  • template(config)
    • Config properties:
      • name {String} The name of the template e.g. /scraps/test (optional in case content exists)
      • path {String} The full path where the templates exists (optional) e.g. /home/../test.tpl
      • content {String} The string content instead of the file content (optional in case name exists & overrides the file content)
      • data {Object} The data object properties (see underscore template)

Task

Prepare your project for development / production using, JSHint & UglifyJS

TBD

Behavior

  • NPM List

    For getting the installed packages I'm using The NPM list command that might throw an error about problems.
    Those problems will be described as part of the NPM information tree. Those errors are not really functional errors but more like packages problems.
    The methods used in this module will still function as expected. I just forward the NPM error. (you can turn the log off if needed) NPM problems:

    • max depth reached This can be handled, if you don't need a really deep information use the 'depth' property to adjust.
    • extraneous It's not on your package.json, if it's installed don't worry about this message.

For more information see the NPM docs

npm loves you