Skip to content

CAPDistributing/cap-js-helpers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CAP Distributing's JavaScript Helpers

Circle CI Badge codecov NSP Status Dependency Status Dev Dependency Status Peer Dependency Status NPM Version MIT License

We created this package so we could easily share these basic function across all of our applications.

Usage

It returns the following functions:

  • sortArray: a function that takes 2 parameters (Array to sort, and key to sort on) and returns a sorted array. For example:
    • var capHelpers = require('cap-helpers');
      var myArray = [{name: 'Zach'}, {name: 'Jamie'}];
      capHelpers.sortArray(myArray, ['name']);
      // returns [{name: 'Jamie'}, {name: 'Zach'}]
  • toBase64: a function that converts the given value to base64. For example:
    • var capHelpers = require('cap-helpers');
      var value = 'this is a string!!';
      capHelpers.toBase64(value);
      // returns 'dGhpcyBpcyBhIHN0cmluZyEh'
  • fromBase64: a function that converts the given value from base64. For example:
    • var capHelpers = require('cap-helpers');
      var value = 'dGhpcyBpcyBhIHN0cmluZyEh';
      capHelpers.fromBase64(value);
      // returns 'this is a string!!'
  • orderObject: a function that orders an object's keys alphabetically. For example:
    • var capHelpers = require('cap-helpers');
      var myObj = {name: 'Jamie', age: 25};
      capHelpers.orderObject(myObj);
      // returns {age: 25, name: 'Jamie'}
  • numberWithCommas: a function that converts a number to a string with commas. For example:
    • var capHelpers = require('cap-helpers');
      var num = 1234567.89;
      capHelpers.numberWithCommas(num);
      // returns '1,234,567.89'
  • camelcaseToUnderscore: a function that takes a camelcased string, object or array and converts it to underscores. For example:
    • var capHelpers = require('cap-helpers');
      var str = 'myString';
      capHelpers.camelcaseToUnderscore(str);
      // returns 'my_string'
      
      // if you pass in an object, it will convert the keys. For example:
      var obj = {thisIsMyKey: 'thisIsMyValue'};
      capHelpers.camelcaseToUnderscore(obj);
      // returns {this_is_my_key: 'thisIsMyValue'}
  • underscoreToCamelcase: a function that takes an underscored string, object or array and converts it to camelcase. For example:
    • var capHelpers = require('cap-helpers');
      var str = 'my_string';
      capHelpers.underscoreToCamelcase(str);
      // returns 'myString'
      
      // if you pass in an object, it will convert the keys. For example:
      var obj = {this_is_my_key: 'this_is_my_value'};
      capHelpers.underscoreToCamelcase(obj);
      // returns {thisIsMyKey: 'this_is_my_value'}
  • underscoreToTitlecase: a function that takes an underscored string, object or array and converts it to titlecase. For example:
    • var capHelpers = require('cap-helpers');
      var str = 'my_string';
      capHelpers.underscoreToTitlecase(str);
      // returns 'MyString'
      
      // if you pass in an object, it will convert the keys. For example:
      var obj = {this_is_my_key: 'this_is_my_value'};
      capHelpers.underscoreToTitlecase(obj);
      // returns {ThisIsMyKey: 'this_is_my_value'}
  • titlecaseToCamelcase: a function that takes a titlecased string, object or array and converts it to camelcase. For example:
    • var capHelpers = require('cap-helpers');
      var str = 'MyString';
      capHelpers.titlecaseToCamelcase(str);
      // returns 'myString'
      
      // if you pass in an object, it will convert the keys. For example:
      var obj = {ThisIsMyKey: 'ThisIsMyValue'};
      capHelpers.titlecaseToCamelcase(obj);
      // returns {thisIsMyKey: 'ThisIsMyValue'}

Testing

  • npm test > Runs all the tests and checks the code coverage.
  • npm run single-test > Runs all the tests once.
  • npm run watch-test > Runs all the tests and watches for changes.
  • npm run check-coverage > Checks the code coverage (this should be ran after running the tests).