The purpose of this library is combine utils functions in one place
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')
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^
const { sleep } = require ( 'sat-utils' ) ;
async function test ( ) {
await sleep ( 2500 ) ;
}
const { isArray } = require ( 'sat-utils' ) ;
// any argument
isArray ( undefined ) ; // => boolean
const { isObject } = require ( 'sat-utils' ) ;
// any argument
isObject ( undefined ) ; // => boolean
const { isRegExp } = require ( 'sat-utils' ) ;
// any argument
isRegExp ( / a/ gi) ; // => boolean
const { isNull } = require ( 'sat-utils' ) ;
// any argument
isNull ( undefined ) ; // => boolean
const { isString } = require ( 'sat-utils' ) ;
// any argument
isString ( undefined ) ; // => boolean
const { isSet } = require ( 'sat-utils' ) ;
// any argument
isSet ( undefined ) ; // => boolean
const { isMap } = require ( 'sat-utils' ) ;
// any argument
isMap ( undefined ) ; // => boolean
const { isUndefined } = require ( 'sat-utils' ) ;
// any argument
isUndefined ( undefined ) ; // => boolean
const { isNumber } = require ( 'sat-utils' ) ;
// any argument
isNumber ( undefined ) ; // => boolean
const { isPromise } = require ( 'sat-utils' ) ;
// any argument
isPromise ( undefined ) ; // => boolean
const { isBuffer } = require ( 'sat-utils' ) ;
// any argument
isBuffer ( undefined ) ; // => boolean
const { isBoolean } = require ( 'sat-utils' ) ;
// any argument
isBoolean ( undefined ) ; // => boolean
const { isSymbol } = require ( 'sat-utils' ) ;
// any argument
isSymbol ( undefined ) ; // => boolean
const { isFunction } = require ( 'sat-utils' ) ;
// any argument
isFunction ( undefined ) ; // => boolean
const { isDate } = require ( 'sat-utils' ) ;
// any argument
isDate ( new Date ( ) ) ; // => boolean
const { isArguments } = require ( 'sat-utils' ) ;
// any argument
isArguments ( new Date ( ) ) ; // => boolean
const { isAsyncFunction } = require ( 'sat-utils' ) ;
// any argument
isAsyncFunction ( undefined ) ; // => boolean
const { isType } = require ( 'sat-utils' ) ;
// any argument
isType ( undefined , 'function' ) ; // => boolean
const { getType } = require ( 'sat-utils' ) ;
// any argument
getType ( undefined ) ; // => string
const { isPrimitive } = require ( 'sat-utils' ) ;
// any argument
isPrimitive ( undefined ) ; // => boolean
const { canBeProxed } = require ( 'sat-utils' ) ;
// any argument
canBeProxed ( undefined ) ; // => boolean
const { toArray } = require ( 'sat-utils' ) ;
const arr1 = toArray ( undefined ) ; // []
const arr2 = toArray ( null ) ; // [null]
const arr3 = toArray ( [ 1 , 2 , 3 ] ) ; // [1,2,3]
const { shuffleArr } = require ( 'sat-utils' ) ;
const arr1 = shuffleArr ( [ 2 , 3 , 1 ] ) ; // [1,3,2]
shuffleArr ( { } ) ; // TypeError 'shuffleArr(): first argument should be an array ...'
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 ...'
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
const { isEmptyArray } = require ( 'sat-utils' ) ;
const isEmpty = isEmptyArray ( [ ] ) ; // true
const isEmpty1 = isEmptyArray ( [ 1 ] ) ; // false
const isEmpty2 = isEmptyArray ( null ) ; // false
const isEmpty3 = isEmptyArray ( { } ) ; // false
const { isNotEmptyArray } = require ( 'sat-utils' ) ;
const isEmpty = isNotEmptyArray ( [ ] ) ; // false
const isEmpty1 = isNotEmptyArray ( [ 1 ] ) ; // true
const isEmpty2 = isNotEmptyArray ( null ) ; // false
const isEmpty3 = isNotEmptyArray ( { } ) ; // false
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
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
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
const { getDirFilesList } = require ( 'sat-utils' ) ;
const files = getDirFilesList ( './node_modules' ) ; // all absolute file pathes in node_module folder
const { safeJSONstringify } = require ( 'sat-utils' ) ;
console . log ( safeJSONstringify ( { } ) ) ;
console . log ( safeJSONstringify ( Object ) ) ;
console . log ( safeJSONstringify ( new Proxy ( ) ) ) ;
const { safeJSONparse } = require ( 'sat-utils' ) ;
console . log ( safeJSONparse ( '{}' ) ) ;
console . log ( safeJSONstringify ( 'dsadas dasda' , { } ) ) ;
const { camelize } = require ( 'sat-utils' ) ;
console . log ( camelize ( 'here is my camel string' ) ) ; // hereIsMyCamelString
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
const { chunkArr } = require ( 'sat-utils' ) ;
chunkArr ( [ 1 , 2 , 3 ] , 2 ) ; // [[1, 3], [2]]
chunkArr ( [ 1 , 2 , 3 ] , 2 , true ) ; // [[1, 2], [3]]
const { millisecondsToMinutes } = require ( 'sat-utils' ) ;
millisecondsToMinutes ( 100_000 ) ; // 1:40
const { getRandomSubString } = require ( 'sat-utils' ) ;
getRandomSubString ( 'abcdefgj' , 3 ) ; // fgf
const { millisecondsToMinutes } = require ( 'sat-utils' ) ;
millisecondsToMinutes ( 100_000 ) ; // 1:40
const { lengthToIndexesArray } = require ( 'sat-utils' ) ;
lengthToIndexesArray ( 3 ) ; // [0,1,2]
const { getRandomNumberFromRange } = require ( 'sat-utils' ) ;
getRandomNumberFromRange ( 1 , 10 ) ; // 9
const { asyncRepeat } = require ( 'sat-utils' ) ;
asyncRepeat ( 5 , async ( ) => {
/* async logic will be executed 5 times */
} ) ;
const { asyncMap } = require ( 'sat-utils' ) ;
asyncMap ( [ 1 , 2 , 3 ] , async ( item , index , arr ) => item + index ) . then ( console . log ) ;
const { asyncForEach } = require ( 'sat-utils' ) ;
asyncForEach ( [ 1 , 2 , 3 ] , async ( item , index , arr ) => console . log ( item , index , arr ) ) ;
const { asyncReduce } = require ( 'sat-utils' ) ;
asyncReduce ( [ 1 , 2 , 3 ] , ( acc , item ) => new Promise ( res => setTimeout ( ( ) => res ( acc + item ) , 25 ) ) , 1 ) . then ( console . log ) ; // 7