Skip to content

Latest commit

 

History

History
482 lines (358 loc) · 10 KB

readme.md

File metadata and controls

482 lines (358 loc) · 10 KB

The purpose of this library is combine utils functions in one place

npm downloads

Content

getRandomArrayItem

const { getRandomArrayItem } = require('sat-utils');
const firstItem = getRandomArrayItem([1, 2, 3, 4]); // 2
const [first, second] = getRandomArrayItem([1, 2, 3, 4], 2); // [3, 1]
getRandomArrayItem([1, 2, 3, 4], 10); // => RangeError('getRandomArrayItem(): more elements taken ...
getRandomArrayItem([]); // => RangeError('getRandomArrayItem(): given array is empty')
getRandomArrayItem(null); // => TypeError 'getRandomArrayItem(): first argument should be an')

getRandomString

const { getRandomString } = require('sat-utils');
const str1 = getRandomString(5); // AsRTl
const str2 = getRandomString(5, { numbers: true }); // 09326
const str3 = getRandomString(5, { lettersAndNumbers: true }); // 0B3a6
const str4 = getRandomString(5, { symbols: true }); // !@#$^
const str5 = getRandomString(5, { lettersNumbersAndSymbols: true }); // a2#B^
const str6 = getRandomString(5, { lowerCase: true }); // abcd^

sleep

const { sleep } = require('sat-utils');
async function test() {
  await sleep(2500);
}

isArray

const { isArray } = require('sat-utils');
// any argument
isArray(undefined); // => boolean

isObject

const { isObject } = require('sat-utils');
// any argument
isObject(undefined); // => boolean

isRegExp

const { isRegExp } = require('sat-utils');
// any argument
isRegExp(/a/gi); // => boolean

isNull

const { isNull } = require('sat-utils');
// any argument
isNull(undefined); // => boolean

isString

const { isString } = require('sat-utils');
// any argument
isString(undefined); // => boolean

isSet

const { isSet } = require('sat-utils');
// any argument
isSet(undefined); // => boolean

isMap

const { isMap } = require('sat-utils');
// any argument
isMap(undefined); // => boolean

isUndefined

const { isUndefined } = require('sat-utils');
// any argument
isUndefined(undefined); // => boolean

isNumber

const { isNumber } = require('sat-utils');
// any argument
isNumber(undefined); // => boolean

isPromise

const { isPromise } = require('sat-utils');
// any argument
isPromise(undefined); // => boolean

isBuffer

const { isBuffer } = require('sat-utils');
// any argument
isBuffer(undefined); // => boolean

isBoolean

const { isBoolean } = require('sat-utils');
// any argument
isBoolean(undefined); // => boolean

isSymbol

const { isSymbol } = require('sat-utils');
// any argument
isSymbol(undefined); // => boolean

isFunction

const { isFunction } = require('sat-utils');
// any argument
isFunction(undefined); // => boolean

isDate

const { isDate } = require('sat-utils');
// any argument
isDate(new Date()); // => boolean

isArguments

const { isArguments } = require('sat-utils');
// any argument
isArguments(new Date()); // => boolean

isAsyncFunction

const { isAsyncFunction } = require('sat-utils');
// any argument
isAsyncFunction(undefined); // => boolean

isType

const { isType } = require('sat-utils');
// any argument
isType(undefined, 'function'); // => boolean

getType

const { getType } = require('sat-utils');
// any argument
getType(undefined); // => string

isPrimitive

const { isPrimitive } = require('sat-utils');
// any argument
isPrimitive(undefined); // => boolean

canBeProxed

const { canBeProxed } = require('sat-utils');
// any argument
canBeProxed(undefined); // => boolean

toArray

const { toArray } = require('sat-utils');

const arr1 = toArray(undefined); // []
const arr2 = toArray(null); // [null]
const arr3 = toArray([1, 2, 3]); // [1,2,3]

shuffleArr

const { shuffleArr } = require('sat-utils');

const arr1 = shuffleArr([2, 3, 1]); // [1,3,2]
shuffleArr({}); // TypeError 'shuffleArr(): first argument should be an array ...'

shuffleArrMutable

const { shuffleArrMutable } = require('sat-utils');

const arr = [1, 2, 3, 4, 5];
shuffleArrMutable(arr);

console.log(arr); // [ 5, 2, 4, 1, 3 ]

shuffleArrMutable({}); // TypeError 'shuffleArr(): first argument should be an array ...'

prettifyCamelCase

const { prettifyCamelCase } = require('sat-utils');

const res1 = prettifyCamelCase(str); // Prettify Camel Case
const res2 = prettifyCamelCase(str, { joinWords: '__' }); // Prettify__Camel__Case
const res3 = prettifyCamelCase(str, { allUpperCase: true, joinWords: '_' }); // PRETTIFY_CAMEL_CASE
const res4 = prettifyCamelCase(str, { firstWordUpperCase: true }); // Prettify camel case

isEmptyArray

const { isEmptyArray } = require('sat-utils');

const isEmpty = isEmptyArray([]); // true
const isEmpty1 = isEmptyArray([1]); // false
const isEmpty2 = isEmptyArray(null); // false
const isEmpty3 = isEmptyArray({}); // false

isNotEmptyArray

const { isNotEmptyArray } = require('sat-utils');

const isEmpty = isNotEmptyArray([]); // false
const isEmpty1 = isNotEmptyArray([1]); // true
const isEmpty2 = isNotEmptyArray(null); // false
const isEmpty3 = isNotEmptyArray({}); // false

isEmptyObject

const { isEmptyObject } = require('sat-utils');

const isEmpty = isEmptyObject({}); // true
const isEmpty1 = isEmptyObject({ a: 1 }); // false
const isEmpty2 = isEmptyObject([]); // false
const isEmpty3 = isEmptyObject([1]); // false
const isEmpty4 = isEmptyObject(null); // false

isEmptyObject

const { isNotEmptyObject } = require('sat-utils');

const isEmpty = isNotEmptyObject({}); // false
const isEmpty1 = isNotEmptyObject({ a: 1 }); // true
const isEmpty2 = isNotEmptyObject([]); // false
const isEmpty3 = isNotEmptyObject([1]); // false
const isEmpty4 = isNotEmptyObject(null); // false

execNumberExpression

const { execNumberExpression } = require('sat-utils');

const isTruly = execNumberExpression('>10', 11); // true
const isTruly1 = execNumberExpression('>10 and <12', 11); // true
const isTruly2 = execNumberExpression('>10 and <13 and !== 12', 11); // true
const isTruly3 = execNumberExpression('<9', 11); // false
const isTruly4 = execNumberExpression('!==11', 11); // false

getDirFilesList

const { getDirFilesList } = require('sat-utils');
const files = getDirFilesList('./node_modules'); // all absolute file pathes in node_module folder

safeJSONstringify

const { safeJSONstringify } = require('sat-utils');
console.log(safeJSONstringify({}));
console.log(safeJSONstringify(Object));
console.log(safeJSONstringify(new Proxy()));

safeJSONparse

const { safeJSONparse } = require('sat-utils');
console.log(safeJSONparse('{}'));
console.log(safeJSONstringify('dsadas dasda', {}));

camelize

const { camelize } = require('sat-utils');
console.log(camelize('here is my camel string')); // hereIsMyCamelString

safeHasOwnPropery

const { safeHasOwnPropery } = require('sat-utils');

safeHasOwnPropery({ a: 1 }, 'a'); // true
safeHasOwnPropery({ a: 1 }, 'b'); // false
safeHasOwnPropery(undefined, 'b'); // false
safeHasOwnPropery(null, 'b'); // false
safeHasOwnPropery(function test() {
  /** */
}, 'name'); // true

chunkArr

const { chunkArr } = require('sat-utils');

chunkArr([1, 2, 3], 2); // [[1, 3], [2]]
chunkArr([1, 2, 3], 2, true); // [[1, 2], [3]]

millisecondsToMinutes

const { millisecondsToMinutes } = require('sat-utils');

millisecondsToMinutes(100_000); // 1:40

getRandomSubString

const { getRandomSubString } = require('sat-utils');

getRandomSubString('abcdefgj', 3); // fgf

millisecondsToMinutes

const { millisecondsToMinutes } = require('sat-utils');

millisecondsToMinutes(100_000); // 1:40

lengthToIndexesArray

const { lengthToIndexesArray } = require('sat-utils');

lengthToIndexesArray(3); // [0,1,2]

getRandomNumberFromRange

const { getRandomNumberFromRange } = require('sat-utils');

getRandomNumberFromRange(1, 10); // 9

asyncRepeat

const { asyncRepeat } = require('sat-utils');

asyncRepeat(5, async () => {
  /* async logic will be executed 5 times */
});

asyncMap

const { asyncMap } = require('sat-utils');

asyncMap([1, 2, 3], async (item, index, arr) => item + index).then(console.log);

asyncForEach

const { asyncForEach } = require('sat-utils');

asyncForEach([1, 2, 3], async (item, index, arr) => console.log(item, index, arr));

asyncReduce

const { asyncReduce } = require('sat-utils');

asyncReduce([1, 2, 3], (acc, item) => new Promise(res => setTimeout(() => res(acc + item), 25)), 1).then(console.log); // 7