jlafer-lib v0.0.10
jlafer-lib
This is a library of helper functions that I find useful.
Installation
npm install --save jlafer-lib
cd jlafer-lib
npm installArray Functions
takeSelected
takeSelected :: ([a], [index]) -> [a]Returns items from the first array whose positions are found in the second array of index values.
const input = [
{name: 'name', value: 'order_num'},
{name: 'length', value: 10},
{name: 'datatype', value: 'string'}
];
takeSelected(input, [0,2]) //=> [{name: 'name', value: 'order_num'}, {name: 'datatype', value: 'string'}]filterOnMatchInOtherList
filterOnMatchInOtherList :: predFn -> [b] -> [a] -> [a]filterOnMatchingKeyInOtherList
filterOnMatchingKeyInOtherList :: string -> [b] -> [a] -> [a]filterOnMatchingSidInOtherList
filterOnMatchingSidInOtherList :: [b] -> [a] -> [a]matchAndMapLists
matchAndMapLists :: (matcherFn, mapperFn) -> ([a] -> [b]) -> [a]Array-of-Object Functions
findObjByKeyVal
findObjByKeyVal :: string -> string -> [object] -> objectThis function will return the first object matched by key and value from the object array argument.
const scores = [
{name: 'alice', value: 9},
{name: 'bob', value: 7},
{name: 'chuck', value: 8}
];
findObjByKeyVal('name', 'bob', scores); //=> {name: 'bob', value: 7}findObjBySid
findObjBySid :: string -> [object] -> objectThis function will return the first object with the sid value matched by the specified string value from the object array argument. This function is a partial application of findObjByKeyVal.
findObjByFriendlyName
findObjByFriendlyName :: string -> [object] -> objectThis function will return the first object with the friendlyName value matched by the specified string value from the object array argument. This function is a partial application of findObjByKeyVal.
findObjByUniqueName
findObjByUniqueName :: string -> [object] -> objectThis curried function will return the first object with the uniqueName value matched by the specified string value from the object array argument. This function is a partial application of findObjByKeyVal.
findMatchingObjByKey
findMatchingObjByKey :: string -> [object] -> object -> objectThis function will return the first item from the object array argument that matches the object argument on the value of the property named by the string argument.
const scores = [
{name: 'alice', value: 9},
{name: 'bob', value: 7},
{name: 'chuck', value: 8}
];
findMatchingObjByKey('name', scores, {name: 'bob', value: 42}); //=> {name: 'bob', value: 7}replaceObjByKey
replaceObjByKey :: string -> [object] -> object -> [object]This curried function will return a copy of the the object array argument, replacing the first object that matches on the key with the object argument.
const scores = [
{name: 'alice', value: 9},
{name: 'bob', value: 7}
];
replaceObjByKey('name', scores, {name: 'bob', value: 12);
//=> [{name: 'alice', value: 9}, {name: 'bob', value: 12}]kvListToObj
kvListToObj :: (string, string) -> [object] -> objectThis function will return an object having properties drawn from the "key-value objects" array supplied. The keys will be taken from the object properties keyed by the first string and the vallues will be taken from the object properties keyed by the second string. It is useful for mapping an array of properties into a dictionary object.
const input = [
{id: 'name', val: 'order_num'},
{id: 'length', val: 10},
{id: 'datatype', val: 'string'}
];
const idValListToObj = kvListToObj('id', 'val');
idValListToObj(input) //=> {name: 'order_num', length: 10, datatype: 'string'}idValueListToObj
idValueListToObj :: [object] -> objectconst input = [
{id: 'name', value: 'order_num'},
{id: 'length', value: 10},
{id: 'datatype', value: 'string'}
];
idValueListToObj(input) //=> {name: 'order_num', length: 10, datatype: 'string'}nameValueListToObj
nameValueListToObj :: [object] -> objectkeyedListToObj
keyedListToObj :: (string, [object]) -> objectconst attributes = [
{name: 'name', value: 'size'},
{name: 'type', value: 'string'},
{name: 'descr', value: 'product size'}
];
keyedListToObj('name', attributes) /* => {
name: {name: 'name', value: 'size'},
type: {name: 'type', value: 'string'},
descr: {name: 'descr', value: 'product size'}});
} */verifyAllItemsHaveKey
verifyAllItemsHaveKey :: (objArr, arrName, key) -> true OR throws ExceptionsumValuesForKey
sumValuesForKey :: string -> [obj] -> floatconst scores = [
{name: 'alice', value: 9},
{name: 'bob', value: 7},
{name: 'chuck', value: 8}
];
sumValuesForKey('value', scores)) //=> 24pickFromAll
pickFromAll :: (keys, list) -> objconst testArrayObjs = [
{sid: 1, uniqueName: 'A1', category: 'A'},
{sid: 2, uniqueName: 'A2', category: 'B'}
]
pickFromAll(['sid', 'category'], testArrayObjs)) //=> [{sid: 1, category: 'A'}, {sid: 2, category: 'B'}]Object Functions
objPropsCnt
objPropsCnt :: object -> integerThis function returns a count of properties in the object argument.
const strings = {
header: 'My Header',
body: 'My Body',
footer: 'My Footer'
};
objPropsCnt(strings) //=> 3mapKeysOfObject
mapKeysOfObject :: mapFirstOfPairFn -> object -> objectThis HOF takes a mapper and will create a function that will transform all keys of an object. The supplied mapper function must transform the first element of a pair array. Such a mapper can be made with the makeMapFirstOfPairFn HOF.
const strings = {
header: 'My Header',
body: 'My Body',
footer: 'My Footer'
};
const mapFirstToUpper = makeMapFirstOfPairFn(R.toUpper);
const mapKeysToUpper = mapKeysOfObject(mapFirstToUpper);
mapKeysToUpper(strings) //=> {HEADER: 'My Header', BODY: 'My Body', FOOTER: 'My Footer'}mapValuesOfObject
mapValuesOfObject :: mapFirstOfPairFn -> object -> objectThis HOF takes a mapper and will create a function that transforms all property values of an object. The supplied mapper function must transform a single value.
const strings = {
header: 'My Header',
body: 'My Body',
footer: 'My Footer'
};
const mapValuesToUpper = mapValuesOfObject(R.toUpper);
mapValuesToUpper(strings) //=> {header: 'MY HEADER', body: 'MY BODY', footer: 'MY FOOTER'}nameValueListToObj
nameValueListToObj :: [object] -> objectGiven an array of "key-value objects", each of which contains name and value properties, this function will return an object having the name values as keys, with associated values taken from the corresponding input value values. This is just kvListToObj('name', 'value').
const input = [
{name: 'name', value: 'order_num'},
{name: 'length', value: 10},
{name: 'datatype', value: 'string'}
];
nameValueListToObj(input) //=> {name: 'order_num', length: 10, datatype: 'string'}Miscellaneous Functions
isNotNil
isNotNil :: a -> booleanisNotNil(42) //=> true
isNotNil({}) //=> true
isNotNil([]) //=> true
isNotNil(null) //=> falseisNotEquals
isNotEquals :: a -> b -> booleanisNotEquals(42, 43) //=> true
isNotEquals(42, null) //=> true
isNotEquals(42, 21*2) //=> false
isNotEquals('foobar', 'foo'+'bar') //=> falsevalueIsObject
valueIsObject :: a -> booleanvalueIsObject({foo: 'bar'}) //=> true
valueIsObject(42) //=> false
valueIsObject([42, 43]) //=> falsevalueNotObject
valueNotObject :: a -> booleanvalueNotObject(42) //=> true
valueNotObject([42, 43]) //=> true
valueNotObject({foo: 'bar'}) //=> falsevalueIsArray
valueIsArray :: a -> booleanvalueIsArray([]) //=> true
valueIsArray([1, 2, 3]) //=> true
valueIsArray({foo: 'bar'}) //=> falsewait
wait :: ms -> any -> Promiseconst identityP = (arg) => Promise.resolve(arg);
identityP('foo')
.then(wait(500))
.then(data => data.toUpper()); //=> Promise('FOO') resolves after 500 mSecDate-Time Functions
formatDuration
formatDuration :: object -> stringconst aDuration = {months: 0, days: 1, hours: 5, minutes: 32, seconds: 17};
formatDuration(aDuration)) //=> '1d 5h 32:17'isoDateToMsec
isoDateToMsec :: isoString -> integer const date1 = new Date(Date.UTC(1970, 0, 1, 0, 0, 0));
isoDateToMsec(date1.toISOString()) //=> 0
const date2 = new Date('December 17, 1990 00:00:00');
isoDateToMsec(date2) //=> 661420800000dtToIsoLocal
dtToIsoLocal :: ([Date || isoString], timezone) -> isoString // format a date, defined in EST, as an ISO-8601-formatted date-time string in PST
const date1 = new Date('December 17, 2018 08:00:00-05:00', 'America/Los_Angeles');
dtToIsoLocal(date1) //=> '2018-12-17T05:00:00-08:00'Filesystem Functions
checkDirExists
checkDirExists :: (path) -> Promise(boolean)copyTextFile
copyTextFile :: (indir, outdir, filename) -> voidLog-Debug Functions
varToStr
varToStr :: any -> stringMath Functions
sumProps
sumProps :: object -> numberconst quarterSales = {q1: 100, q2: 90, q3: 120, q4: 130};
sumProps(quarterSales); //=> 440
const noSales = {};
sumProps(noSales); //=> 0getKeyOfMaxProp
getKeyOfMaxProp :: object -> stringconst quarterSales = {q1: 100, q2: 90, q3: 120, q4: 130};
getKeyOfMaxProp(quarterSales); //=> 130
const noSales = {};
getKeyOfMaxProp(noSales); //=> ''pad
pad :: (n, width, z) -> stringconst singleDigit = 5;
pad(singleDigit, 3); //=> '005'
pad(singleDigit, 5, '.'); //=> '....5'zeroPad2
zeroPad2 :: number -> stringzeroPad2(3); //=> '03'
zeroPad2(42); //=> '42'
zeroPad2(0); //=> '00'round
round :: (precision, float) -> stringconst singleDigit = 5;
round(0, 342.5378); //=> 343
round(2, 342.5378); //=> 342.54Sequence
Sequence.getNextValue :: () -> number
Sequence.getCrrentValue :: () -> numberCommon Helper Functions
makeMapFirstOfPairFn
makeMapFirstOfPairFn :: mapFn -> pair -> pairThis HOF takes a mapper and will create a function that, given a pair (i.e., an array of length two), will transform the first element of the pair. It is useful for mapping an array of pairs - often one created from an object's keys and values. See mapKeysOfObject for an example of this usage.
const strings = ['hdr', 'My Heading'];
const mapFirstToUpper = makeMapFirstOfPairFn(R.toUpper);
mapFirstToUpper(strings) //=> ['HDR', 'My Heading']String Functions
makePathList
makePathList :: string -> [string]const path = 'worker.attributes.routing';
makePathList(path)) //=> ['worker', 'attributes', 'routing']stripReturnAndSplitOnComma
stripReturnAndSplitOnComma :: string -> stringThis function is useful for selecting the field names from the header line of a CSV file.
stripReturnAndSplitOnComma('a,b,c,d,e\n') //=> ['a','b','c','d','e']trimLeadingZeroes
trimLeadingZeroes :: string -> stringtrimLeadingZeroes('0042)) //=> '42'