Skip to content

Repository files navigation

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

npm downloads

Content

getRandomArrayItem

const{ getRandomArrayItem }=require('sat-utils');constfirstItem=getRandomArrayItem([1,2,3,4]);// 2const[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');conststr1=getRandomString(5);// AsRTlconststr2=getRandomString(5,{numbers: true});// 09326conststr3=getRandomString(5,{lettersAndNumbers: true});// 0B3a6conststr4=getRandomString(5,{symbols: true});// !@#$^conststr5=getRandomString(5,{lettersNumbersAndSymbols: true});// a2#B^conststr6=getRandomString(5,{lowerCase: true});// abcd^

sleep

const{ sleep }=require('sat-utils');asyncfunctiontest(){awaitsleep(2500);}

isArray

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

isObject

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

isRegExp

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

isNull

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

isString

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

isSet

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

isMap

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

isUndefined

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

isNumber

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

isPromise

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

isBuffer

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

isBoolean

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

isSymbol

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

isFunction

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

isDate

const{ isDate }=require('sat-utils');// any argumentisDate(newDate());// => boolean

isArguments

const{ isArguments }=require('sat-utils');// any argumentisArguments(newDate());// => boolean

isAsyncFunction

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

isType

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

getType

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

isPrimitive

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

canBeProxed

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

toArray

const{ toArray }=require('sat-utils');constarr1=toArray(undefined);// []constarr2=toArray(null);// [null]constarr3=toArray([1,2,3]);// [1,2,3]

shuffleArr

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

shuffleArrMutable

const{ shuffleArrMutable }=require('sat-utils');constarr=[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');constres1=prettifyCamelCase(str);// Prettify Camel Caseconstres2=prettifyCamelCase(str,{joinWords: '__'});// Prettify__Camel__Caseconstres3=prettifyCamelCase(str,{allUpperCase: true,joinWords: '_'});// PRETTIFY_CAMEL_CASEconstres4=prettifyCamelCase(str,{firstWordUpperCase: true});// Prettify camel case

isEmptyArray

const{ isEmptyArray }=require('sat-utils');constisEmpty=isEmptyArray([]);// trueconstisEmpty1=isEmptyArray([1]);// falseconstisEmpty2=isEmptyArray(null);// falseconstisEmpty3=isEmptyArray({});// false

isNotEmptyArray

const{ isNotEmptyArray }=require('sat-utils');constisEmpty=isNotEmptyArray([]);// falseconstisEmpty1=isNotEmptyArray([1]);// trueconstisEmpty2=isNotEmptyArray(null);// falseconstisEmpty3=isNotEmptyArray({});// false

isEmptyObject

const{ isEmptyObject }=require('sat-utils');constisEmpty=isEmptyObject({});// trueconstisEmpty1=isEmptyObject({a: 1});// falseconstisEmpty2=isEmptyObject([]);// falseconstisEmpty3=isEmptyObject([1]);// falseconstisEmpty4=isEmptyObject(null);// false

isEmptyObject

const{ isNotEmptyObject }=require('sat-utils');constisEmpty=isNotEmptyObject({});// falseconstisEmpty1=isNotEmptyObject({a: 1});// trueconstisEmpty2=isNotEmptyObject([]);// falseconstisEmpty3=isNotEmptyObject([1]);// falseconstisEmpty4=isNotEmptyObject(null);// false

execNumberExpression

const{ execNumberExpression }=require('sat-utils');constisTruly=execNumberExpression('>10',11);// trueconstisTruly1=execNumberExpression('>10 and <12',11);// trueconstisTruly2=execNumberExpression('>10 and <13 and !== 12',11);// trueconstisTruly3=execNumberExpression('<9',11);// falseconstisTruly4=execNumberExpression('!==11',11);// false

getDirFilesList

const{ getDirFilesList }=require('sat-utils');constfiles=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(newProxy()));

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');// truesafeHasOwnPropery({a: 1},'b');// falsesafeHasOwnPropery(undefined,'b');// falsesafeHasOwnPropery(null,'b');// falsesafeHasOwnPropery(functiontest(){/** */},'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)=>newPromise(res=>setTimeout(()=>res(acc+item),25)),1).then(console.log);// 7

getStringEqualtyPersentage

const{ getStringEqualtyPersentage }=require('sat-utils');constpercentage=getStringEqualtyPersentage('asd','asd');// 100.0

stringifyData

const{ stringifyData }=require('sat-utils');constobj={b: [{b: 1}],c: {c: ()=>console.log('stringifyData'),},};conststr=stringifyData(data);// {b: [{b: 1}], c: {c: () => console.log('stringifyData')}}

About

No description, website, or topics provided.

Resources

Stars

5 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages