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

caro

v2.1.1

Published

General libraries for JavaScript / Node.js

Downloads

30

Readme

Caro.js

Build Status

General libraries for JavaScript / Node.js
And also can extend from lodash

Install

In Html

<script src="/js/caro.js"></script>
<script>
var arr = [1, 2, 3];
caro.cleanArr(arr); // []
</script>

In Html with lodash

<script src="/js/lodash.js"></script>
<script src="/js/caro.js"></script>
<script>
// You can use caro as lodash
var arr = [1, 2, 3];
caro.cleanArr(arr); // []
caro.isArray(arr); // true
</script>

In Node.js

$ npm install caro
var arr = [1, 2, 3];
caro.cleanArr(arr); // []

In Node.js with lodash

$ npm install lodash
$ npm install caro
// You can use caro as lodash
var arr = [1, 2, 3];
caro.cleanArr(arr); // []
caro.isArray(arr); // true

Index

Array | Helper | Loop | Object | Path | String | TypeCheck | TypeCover

Array

Back to Index

  • cleanArr(arr) - remove all items in array
var arr = [1, 2];
caro.cleanArr(arr); // []
  • pushNoDuplicate(arr, val...) - push value to array exclude duplicate
var arr = [1, 2, 3];
var r = caro.pushNoDuplicate(arr, 1, 3, {}, {}, 3); // [1, 2, 3, {}, {}]
  • pushNoEmptyVal(arr, val...) - push non-empty value to array
var arr = [1, 2, 3];
var r = caro.pushNoEmptyVal(arr, 1, 'caro', {}, undefined, null, 0, '', []); // [1, 2, 3, 1, 'caro', 0]
  • pullEmptyVal(arr) - pull empty-value from array
var arr = [1, '', null, 'caro'];
var r = caro.pullEmptyVal(arr);
console.log(arr); // [1, 'caro']
console.log(r); // ['', null]
  • pullUnBasicVal(arr) - pull un-basic-value(exclude boolean/string/number) from array
var arr = [1, {a: 1}, 'caro'];
var r = caro.pullUnBasicVal(arr);
console.log(arr); // [1, 'caro']
console.log(r); // [{a: 1}]
  • randomPick(arr [removeFromArr=false]) - select item from array by random
var arr = [1, 2, 3];
var arr2 = [1, 2, 3];
var r = caro.randomPick(arr); // r should be an item of arr, and arr is not changed
var r2 = caro.randomPick(arr2, true); // r2 should be an item of arr2, and arr2 should has not it
  • sumOfArr(arr [force=false]) - get sum-value in array
var arr = [1, 2, '5'];
var r = caro.sumOfArr(arr); // 3
var r2 = caro.sumOfArr(arr, true); // 8

Helper

Back to Index

  • executeIfFn(fn [arg...]) - execute it if argument is function
var arg = function (i) {
    return ++i;
};
var arg2 = null;
var r = caro.executeIfFn(arg, 12); // 13
var r2 = caro.executeIfFn(arg2); // undefined
  • formatMoney(str [type | opt]) - format string/number to money type
var r = caro.formatMoney(null); // '0' 
var r2 = caro.formatMoney('12003000.923', 'int'); // '12,003,000'
var r3 = caro.formatMoney(12003000.923, 'sInt'); // '$12,003,000'
var r4 = caro.formatMoney(12003000.923, {
  float: 0, separated: ',',
  decimal: '.', prefix: '',
  forceFloat: false
}); // '12,003,000' - here is default options
var r5 = caro.formatMoney(12003000.923, {
  float: 5, forceFloat: true
}); // '12,003,000.92300'
  • getFnName(fn) - get function name
var arg = function (i) {
    return ++i;
};
var arg2 = function isFn(){};
var r = caro.getFnName(arg); // ''
var r2 = caro.getFnName(arg2); // 'isFn'
  • getFnBody(fn) - get function body
var arg = function (i) {
    return ++i;
};
var r = caro.getFnBody(arg); // '\n    return ++i;\n    '
  • getStackList([start=0] [length=0]) - get stack-information list
// in /caro/caro.js
var r = caro.getStackList();
/*
[{ 
    stack: 'Context.<anonymous> (/caro/caro.js:1:14)'
    method: 'Context.<anonymous>',
    path: '/caro/caro.js',
    line: '1',
    position: '14',
    file: 'caro.coffee' },
    ...
    ...
]
*/
  • setInterval(fn, ms [excludeTimes = 0]) - easy-use for setInterval
var countA = 0
var countB = 0
caro.setInterval(function() {
  ++countA
  console.log('A Run ' + countA + ' times');
}, 300, 3); // exclude function 3 times, each 300 milliseconds
caro.setInterval(function() {
  if(countB === 4){
    return false;
  }
  ++countB
  console.log('B Run ' + countB + ' times');
}, 300); // exclude function each 300 milliseconds, and stop when countB === 4
  • random(len [opt]) - create random string
var r = caro.random(15); // e.g. '6EJDRlBy6Z25s2O'
var r2 = caro.random(15, {
  lower: true,
  upper: true,
  num: true,
  exclude: ''
}); // here is default options
var r3 = caro.random(15, {
  lower: false,
  upper: true,
  num: false,
  exclude: 'A,B,C'
}); // - will create uppercase-only, and without A/B/C
  • randomInt(max [min = 0]) - random an integer
var r = caro.randomInt(3); // integer from 0 to 3
var r2 = caro.randomInt(3, -3); // integer from -3 to 3
var r3 = caro.randomInt(); // 0
  • randomNum(max [min = 0]) - random a number
var r = caro.randomNum(3); // number from 0 to 3
var r2 = caro.randomNum(3, -3); // number from -3 to 3
var r3 = caro.randomNum(); // 0
  • serializeUrl(str [oArgs] [coverEmpty=false]) - format object to URL params
var arg = 'http://localhost';
var obj = {a: 1, b: 2, c: null};
var r = caro.serializeUrl(arg, obj); // 'http://localhost?a=1&b=2'
var r2 = caro.serializeUrl(arg, obj, true); // 'http://localhost?a=1&b=2&c='

Loop

Back to Index

  • loop(fn, start=0, end=0 [step=1]) - for-loop
caro.loop(function (i){
    // i = 10, 9,...0
}, 10, 0);
caro.loop(function (i){
    if(i > 5) return false;
    // i = 1, 3, 5
}, 1, 10, 2);

Object

Back to Index

  • assignByKeys(obj, obj2, keys [replace=true]) - assign elements to from obj2 to obj1 by keys
var obj = {a: 1, b: 2, c: 3, d: 4};
var obj2 = {d: 1, e: 2, f: 3};
caro.assignByKeys(obj, obj2, ['d', 'f']);
// obj = {a: 1, b: 2, c: 3, d: 1, f: 3}
obj = {a: 1, b: 2, c: 3, d: 4};
caro.assignByKeys(obj, obj2, ['d', 'f'], false);
// obj = {a: 1, b: 2, c: 3, d: 4, f: 3}
  • catching(obj, obj2...) - catch other object-values to target-object when it has key matched
var obj = {name: 'Caro', age: 18};
var obj2 = {country: 'Taiwan', city: 'Kaohsiung'};
var r = {name: ''};
var r2 = {name: '', city: ''};
caro.catching(r, obj); // {name: 'Caro'}
caro.catching(r2, obj, obj2); // {name: 'Caro', city: 'Kaohsiung'}
  • classify(obj) - group by argument type
var r = caro.classify({
  a: 1,
  b: 'd',
  c: {cc: 1},
  d: function(){},
  e: ['caro'],
  f: 'FF',
});
/*
{ 
  bool: [],
  str: [ 'd', 'FF' ],
  num: [ 1 ],
  arr: [ [ 'caro' ] ],
  obj: [ { cc: 1 } ],
  fn: [ [Function] ] 
}
*/
  • differentKeys(obj, obj2 [reverse=false]) - get keys that obj has but obj2 not
var obj = {name: 'Caro', age: 18};
var obj2 = {name: 'Snoopy', country: 'Taiwan', city: 'Kaohsiung'};
var r = caro.differentKeys(obj, obj2); // ['age']
var r2 = caro.differentKeys(obj, obj2, true); // ['country', 'city']
  • hasEqualKeys(obj, obj2) - check if all keys are equal between objects
var obj = {name: 'Caro', age: 18};
var obj2 = {name: 'Snoopy', age: 3};
var obj3 = {name: 'Kitty', country: 'Japan'};
var r = caro.hasEqualKeys(obj, obj2); // true
var r2 = caro.hasEqualKeys(obj, obj3); // false
  • sameKeys(obj, obj2) - get keys that is same between objects
var obj = {a: 1, b: 2, c: 3, e: 4};
var obj2 = {a: 3, c: 4, d: 5, e: 6};
var r = caro.sameKeys(obj, obj2); // ['a', 'c', 'e']

Path

Back to Index

  • getDirPath(path) - get dir-path
var r = caro.getDirPath('a/b/c/d.js'); // 'a/b/c/'
var r2 = caro.getDirPath('a/b/a'); // 'a/b/'
  • getFileName(path [getFull=true]) - get filename of path
var r = caro.getFileName('a/b/c/d.js'); // 'd.js'
var r2 = caro.getFileName('a/b/c/d.js', false); // 'd'
var r3 = caro.getFileName('a/b/a'); // 'a'
  • getExtendName(path [getFull=true]) - get extend-name of filename
var r = caro.getExtendName('a/b/c/d.js'); // '.js'
var r2 = caro.getExtendName('a/b/c/d.js', false); // 'js'
var r3 = caro.getExtendName('a/b/a'); // ''

String

Back to Index

  • addHead(str, addStr) - add prefix-string if not exists
var r = caro.addHead('moon', 'mo'); // 'moon'
var r2 = caro.addHead('Moon', 'is'); // 'isMoon'
  • addTail(str, addStr) - add suffix-string if not exists
var r = caro.addTail('moon', 'on'); // 'moon'
var r2 = caro.addTail('moon', 'Day'); // 'moonDay'
  • brToWrap(str, addStr) - cover '<br />'to wrap
var r = caro.brToWrap('this is<br />wrap content.'); // 'this is\nwrap content.'
  • insertStr(str1, str2, [position]) - insert string to another
var r = caro.insertStr('Lift is good', ' so', 7); // 'Lift is so good'
var r2 = caro.insertStr('I love my', ' dog'); // 'I love my dog' 
  • lowerStr(str [start = 0 or callback] [end]) - cover string to lowercase
var r = caro.lowerStr('I AM CARO'); // 'i am caro'
var r2 = caro.lowerStr('I AM CARO', 5); // 'I AM caro'
var r3 = caro.lowerStr('I AM CARO', 5, 6); // 'I AM cARO'
var r4 = caro.lowerStr('I AM CARO', function(letter){
  if(letter !== 'C'){
    return true;
  }
}); // 'i am Caro'
var r5 = caro.lowerStr('I AM CARO', function(letter, i){
  if(i > 1){
    return true;
  }
}); // 'I am caro'
  • replaceAll(str, find, replace) - replace string that matched
var r = caro.replaceAll('I*am*{Caro}.', '*', '-'); // 'I-am-{Caro}.'
var r2 = caro.replaceAll('I-am-Caro.', '-', '@'); // 'I@am@Caro.'
  • replaceLast(str, find, replace) - replace string that last-matched
var r = caro.replaceLast('I-am-Caro.', '-', ' '); // 'I-am Caro.'
var r2 = caro.replaceLast('I am Caro not Colo.', 'C', 'T'); // 'I am Caro not Tolo.'
  • splitByWrap(str) - split string by wrap
var r = caro.splitByWrap('''
    I love
    my mother
    and
    my father
'''); // [ 'I love', 'my mother', 'and', 'my father' ]
  • splitStr(str [splitter]) - split string
var r = caro.splitStr('i am caro', ' '); // ['i', 'am', 'caro']
var r2 = caro.splitStr('I ~love Snoopy !~!', ['~', ' ']); // ['I', '', 'love', 'Snoopy', '!', '!']
  • strToBool(str) - return false if string like'false', otherwise return true
var r = caro.strToBool('false'); // false
var r2 = caro.strToBool('fAlse'); // false
var r3 = caro.strToBool('123'); // true
var r4 = caro.strToBool(''); // false
  • upperStr(str [start = 0 or callback] [end]) - cover string to uppercase
var r = caro.upperStr('i am caro'); // 'I AM CARO'
var r2 = caro.upperStr('i am caro', 5); // 'i am CARO'
var r3 = caro.upperStr('i am caro', 5, 6); // 'i am Caro'
var r4 = caro.upperStr('i am caro', function(letter){
  if(letter === 'i' or letter === 'c'){
    return true;
  }
}); // 'I am Caro'
var r5 = caro.upperStr('i am caro', function(letter, i){
  if(i < 1){
    return true;
  }
}); // 'I am caro'
  • wrapToBr(str, addStr) - cover wrap to '<br />'
var r = caro.wrapToBr(''''
  this is
  wrap content.
'''); // 'this is<br />wrap content.'

TypeCheck

Back to Index

  • isBasicVal(arg...) - check if basic-value (boolean/string/number)
var r = caro.isBasicVal(1, '', false); // true
var r2 = caro.isBasicVal({}); // false
  • isEmptyVal(arg...) - check if empty-value ( {} | [] | null | '' | undefined | NaN )
var r = caro.isEmptyVal(0); // false
var r2 = caro.isEmptyVal({}); // true
  • isEasingTrue(arg...) - check if true or 'true' or 1
var r = caro.isEasingTrue('True'); // true
var r2 = caro.isEasingTrue(1); // true
  • isEasingFalse(arg...) - check if false or 'false' or 0
var r = caro.isEasingFalse('false'); // true
var r2 = caro.isEasingFalse(0); // true
  • isJson(arg...) - check if JSON
var r = caro.isJson(null); // true
var r2 = caro.isJson('caro'); // false
var r3 = caro.isJson('{"a":1}') // true
var r4 = caro.isJson('{"a":1, "b": function(){}}') // false
  • isObjJson(arg...) - check if JSON first, then check if object-type
var r = caro.isObjJson('{"a": 1}'); // true
var r2 = caro.isObjJson('{"a": function(){}}'); // false
var r3 = caro.isObjJson({b: 2}); // false
  • isUpper(str) - check string if all uppercase
var r = caro.isUpper('CARO'); // true
var r2 = caro.isUpper('caro'); // false
  • isLower(str) - check string if all lowercase
var r = caro.isLower('caro'); // true
var r2 = caro.isLower('Caro'); // false

TypeCover

Back to Index

  • toStr(arg) - cover to string
var r = caro.toStr('3'); // '3'
var r2 = caro.toStr(['caro', undefined]); // 'caro,'
var r3 = caro.toStr({
  a: false,
  b: null,
  c: 0,
  d: NaN,
  e: undefined,
  f: [],
  g: function() {}
}); // '[object Object]'
var r4 = caro.toStr(null); // 'null'
var r5 = caro.toStr(undefined); // 'undefined'
var r6 = caro.toStr(function () { }); // 'function () { }'
  • toInt(arg) - cover to integer
var r = caro.toInt('3'); // 3
var r2 = caro.toInt('3.2'); // 3
var r3 = caro.toInt('caro'); // NaN
var r4 = caro.toNum({}); // NaN
var r5 = caro.toInt(null); // NaN
var r6 = caro.toInt(undefined); // NaN
var r7 = caro.toInt(function () { }); // NaN
  • toNum(arg) - cover to number
var r = caro.toNum('3'); // 3
var r2 = caro.toNum('3.2'); // 3.2
var r3 = caro.toNum('caro'); // NaN
var r4 = caro.toNum({}); // NaN
var r5 = caro.toNum(null); // 0
var r6 = caro.toNum(undefined); // NaN
var r7 = caro.toNum(function () { }); // NaN
  • toFixedNum(arg [dec=2]) - cover to fixed-number
var r = caro.toFixedNum('3.4355'); // 3.44
var r2 = caro.toFixedNum(2.12345, 3); // 2.123
var r3 = caro.toFixedNum('caro', 3) // NaN
  • toJson(arg [replacer=null] [space=0]) - cover to JSON
var arg = {a: 3, b: 5};
var replacer = function (key, val) {
  if (key === '') {
    return val; // the arg itself
  }
  return val + 1;
};
var r = caro.toJson(3.4); // '3.4'
var r2 = caro.toJson(arg, replacer); // '{"a":4,"b":6}'

History

  • refine some functions replaced by lodash - v2.1.0
  • No more support if less than node.js v6 - v2.0.0
  • Update [Loop -> loop] - v1.0.1
  • Be independent from lodash - v1.0.0
  • Add [Helper -> randomNum] - v0.24.9
  • Add [Helper -> setInterval] - v0.23.6
  • Move [String -> random to Helper -> random] - v0.22.6
  • Update [String -> upperStr] - v0.22.5
  • Update [String -> lowerStr] - v0.22.4
  • Remove [Object -> toWord] - v0.22.3
  • Update CLI setting - v0.21.3
  • Add [Array -> randomPick] - v0.21.2
  • Add [Helper -> randomInt] - v0.20.2
  • Update [Object -> toWord] - v0.19.2
  • Update Package - v0.19.1
  • Add [Array -> cleanArr] - v0.19.0
  • Add [String -> insertStr] - v0.18.0
  • Add [Object -> assignByKeys] - v0.17.0
  • Fix version - v0.16.1
  • Add [Object -> sameKeys] - v0.16.0
  • Rename [Object -> equalKeys to hasEqualKeys] - v0.15.1