1.0.0 • Published 1 year ago

pro-underscore v1.0.0

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

Add common methods in Array,Object and String prototype

  • pro-underscore added some helper functions with underscore(_) prefix into prototype of Array/String/Object.
  • All methods are immutable.

Installation

npm i pro-underscore --save
//or with yarn
yarn add pro-underscore

Usage

//CommonJS
const proto = require('pro-underscore');
proto.init({Array:true,Object:true,String:true});
//ES6
import proto from 'pro-underscore'
proto.init({Array:true,Object:true,String:true});

Note

  • If no arguments are passed, all functions will be added to the Array, Object, and String prototypes.
  • If you pass an empty object({}) as an argument to the init method, functions will not be added to the prototype.
  • If you only want to add functions to an array prototype, use the init function like proto.init({Array:true,Object:false,String:fasle}); or proto.init({Array:true});
  • In the package's init method, use Array,Object,String instead of array,object,string as the key of the passed Object. If you enter the wrong key, the methods will not be added to the prototype.

Methods

String Method

MethodDescription
_capitalizecapitalizes the first letter of the string
_toTitleCaseconvert first letter of each word capitalized
_toSwapCaseall letters changed to uppercase or lowercase depending on current case of letter (upper or lower)
_toCamelCaseconvert string into camel Case
_toPascalCaseconvert string into pascal Case
_toDotCaseconvert string into dot case. e.g hello.world
_toSlugCaseconvert string into slug case. e.g hello-world
_truncatetruncates the given string to the specified length
_maskmasks a portion of a string with a repeated character
_isJsonchecks if the string is a valid JSON string or not
_camelToSnakeCaseconverts a camelCase string to snake case string
_charsconvert string to array of characters
_escapeescape string for use in HTML attributes.
_unescapeunescape string for use in HTML attributes.
_wordsconvert string to array of words

Object Method

MethodDescription
_flipTo swap the keys and values of an object
_sortsort an object by keys
_pickpick specific keys from an object
_omitomit specific keys from an object
_clonedeep clone
_mergemerge multiple object
_keys_countget the size of an object like number of keys
_keysget all keys from an object. it's equal to Object.keys()
_valuesget all values from an object. it's equal to Object.values()
_capitalizeKeysconvert all keys to title case
_toLowerCaseKeysconvert all keys to lower case
_toUpperCaseKeysconvert all keys to upper case

Array Method

MethodDescription
_capitalizeconvert each element to capitalize if element is a string
_toTitleCaseconvert each element to title case if element is a string
_toLowerCaseconvert each element to lower case if element is a string
_toUpperCaseconvert each element to lower case if element is a string
_toCamelCaseconvert each element to lower case if element is a string
_toPascalCaseconvert each element to pascal case if element is a string
_toSnakeCaseconvert each element to snake case if element is a string
_toSlugCaseconvert each element to slug case if element is a string
_sumsum of all integer element in the array
_minfind min number with the array
_maxfind max number with the array
_randomget random element of the array
_chunkcreate chunk of array
_isEquivalentcheck the array is equivalent to another array
_headget first element of array
_lastget last element of array
_fromPairsTo swap the keys and values in an object
_hashCodeTo swap the keys and values in an object
_insertinsert data into specific index
_deleteAtdelete data from specific index
_atget element data from specific index

Examples

String Methods

  • _capitalize
const word = "hello world";
const updatedWord = word._capitalize();
console.log(updatedWord); // output: Hello world
  • _toTitleCase
const word = "hello world";
const updatedWord = word._toTitleCase();
console.log(updatedWord); // output: Hello World
  • _toSwapCase
const word = "hello World";
const updatedWord = word._toSwapCase();
console.log(updatedWord); // output: HELLO wORLD
  • _toCamelCase
const word = "Hello World";
const updatedWord = word._toCamelCase();
console.log(updatedWord); // output: helloWorld
  • _toPascalCase
const word = "hello World";
const updatedWord = word._toPascalCase();
console.log(updatedWord); // output: HelloWorld
  • _toDotCase
const word = "hello World";
const updatedWord = word._toDotCase();
console.log(updatedWord); // output: hello.World
  • _toSnakeCase
const word = "hello World";
const updatedWord = word._toSnakeCase();
console.log(updatedWord); // output: hello_world
  • _toSlugCase
const word = "hello World";
const updatedWord = word._toSlugCase();
console.log(updatedWord); // output: hello-world
  • _truncate
const word = "hello World";
const updatedWord = word._truncate(5);
console.log(updatedWord); // output: hello...
  • _mask
const word = "hello World";
const updatedWord = word._mask(5, "*");
console.log(updatedWord); // output: hello******
  • _isJson
const word = "{'hello':'hello','world':'world'}";
const updatedWord = word._isJson();
console.log(updatedWord); // output: true
  • _camelToSnakeCase
const word = "helloWorld";
const updatedWord = word._camelToSnakeCase();
console.log(updatedWord); // output: hello_world
  • _chars
const word = "hello World";
const updatedWord = word._chars();
console.log(updatedWord); // output: ["h", "e", "l", "l", "o", " ", "W", "o", "r", "l", "d"]
  • _words
const word = "hello World";
const updatedWord = word._words();
console.log(updatedWord); // output: ["hello", "World"]
  • _toTitleCase
const word = "hello World";
const updatedWord = word._toTitleCase();
console.log(updatedWord); // output: Hello World

Object Methods

  • _flip
const object = { a: 1, b: 2 };
const updatedObject = object._toTitleCase();
console.log(updatedObject); // output: { 1: "a", 2: "b" }
  • _sort
const object = { a: 2, b: 1 };
const updatedObject = object._sort();
console.log(updatedObject); // output: { b:1,a:2 };
  • _pick
const object = { a: 1, b: 2 };
const updatedObject = object._pick(["a"]);
console.log(updatedObject); // output: {a:1}
  • _omit
const object = { a: 1, b: 2 };
const updatedObject = object._omit(["a"]);
console.log(updatedObject); // output: {b:2}
  • _clone
const object = { a: 1, b: 2 };
const cloneObject = object._clone();
console.log(cloneObject); // output: { a: 1, b: 2 }
  • _merge
const object = { a: 1, b: 2 };
const mergedObject = object._merge({ a: 3, c: 4 });
console.log(mergedObject); // output: { a: 3, b: 2,c:4 }
  • _keys_count
const object = { a: 1, b: 2, c: 4 };
const sizeofObject = object._keys_count();
console.log(sizeofObject); // output: 3
  • _keys
const object = { a: 1, b: 2, c: 4 };
const objectKeys = object._keys();
console.log(objectKeys); // output: [a,b,c]
  • _values
const object = { a: 1, b: 2, c: 4 };
const objectValues = object._values();
console.log(objectValues); // output: [1,2,4]
  • _capitalizeKeys
const object = { HelloWord: "HelloWord", capWord: "capWord" };
const updateObject = object._capitalizeKeys();
console.log(updateObject); // output: { "helloWord": "HelloWord","capWord":"capWord"};;
  • _toLowerCaseKeys
const object = { HelloWord: "HelloWord", capWord: "capWord" };
const updateObject = object._toLowerCaseKeys();
console.log(updateObject); // output: { "Helloword": "HelloWord","capword":"capWord"}
  • _toUpperCaseKeys
const object = { HelloWord: "HelloWord", capWord: "capWord" };
const updateObject = object._toUpperCaseKeys();
console.log(updateObject); // output: { "HELLOWORD": "HelloWord","CAPWORD":"capWord"}

Array Methods

  • _capitalize
const array = [1, 2, 3, "4", "a", "hello", "WORD", "lETTER"];
const updateArray = array._capitalize();
console.log(updateArray); // output: [1, 2, 3, "4", "A", "Hello", "WORD", "LETTER"]
  • _toLowerCase
const array = [
  1,
  2,
  3,
  "4",
  [1, "3"],
  { a: 1 },
  "A",
  "hello",
  "WORD",
  "LeTtER",
];
const updateArray = array._toLowerCase();
console.log(updateArray); // output: [1,2,3,"4",[1, "3"],{ a: 1 }, "a", "hello","word", "letter"]
  • _toUpperCase
const array = [
  1,
  2,
  3,
  "4",
  [1, "3"],
  { a: 1 },
  "A",
  "hello",
  "WORD",
  "LeTtER",
];
const updateArray = array._toUpperCase();
console.log(updateArray); // output: [1,2,3,"4",[1, "3"],{ a: 1 },"A", "HELLO","WORD","LETTER"]
  • _sum
const array = [
  1,
  2,
  3,
  "4",
  [1, "3"],
  { a: 1 },
  "A",
  "hello",
  "WORD",
  "LeTtER",
];
const sum = array._sum();
console.log(sum); // output:6
  • _min
const array = [
  1,
  2,
  3,
  "4",
  [1, "3"],
  { a: 1 },
  "A",
  "hello",
  "WORD",
  "LeTtER",
];
const min = array._min();
console.log(min); // output:1
  • _chunk
const array = [
  1,
  2,
  3,
  "4",
  [1, "3"],
  { a: 1 },
  "A",
  "hello",
  "WORD",
  "LeTtER",
];
const chunk = array._chunk(5);
console.log(chunk); // output:[[1,2,3,"4",[1, "3"]],[{ a: 1 }, "A","hello","WORD", "LeTtER"]]
  • _unique
const array = [
  1,
  2,
  3,
  "4",
  [1, "3"],
  2,
  "4",
  { a: 1 },
  { a: 1 },
  null,
  undefined,
  [8],
  "a",
  "A",
  "a",
  "LeTTer",
  [8],
  "LeTTer",
];
const chunk = array._unique();
console.log(chunk); // output:[1,2,3,"4",[1,"3"],{a:1},null, undefined,[8],"a", "A","LeTTer"]