Bscotch Utilities
Utility and helper methods for common programming problems in Node.js.
(These utilities do not work in browsers.)
Installation
Requirements
Installation
npm install @bscotch/utility
Usage
Typescript:
import {method} from "@bscotch/utility"
JavaScript:
const {method} = require('@bscotch/utility');
Utility Types
import {
HttpMethod,
EmptyArray,
PartialBy,
RequiredBy,
UnwrappedPromise,
AnyFunction,
Nullish,
NotNullish,
NotNull,
Defined,
ExtractKeysByValue,
ExcludeKeysByValue,
PickByValue,
OmitByValue,
} from '@bscotch/utility';
const nothingHere: EmptyArray = ["hello"];
type MixedPartial = PartialBy<{wasRequired:boolean, stillRequired:number},"soonNotRequired">;
type PromiseContent = UnwrappedPromise<Promise<"hello">>;
Strings
import {
capitalize,
decodeFromBase64,
encodeToBase64,
decodeFromBase64JsonString,
encodeToBase64JsonString,
explode,
nodent,
oneline,
undent,
} from '@bscotch/utility';
oneline`
This string
will be converted
into
one that is
on a single line.
`;
undent`
All lines will
be un-inindented
based on the line
with the smallest indentation.
`;
nodent`
All lines will
be un-inindented
completely
but still on separate lines.
`;
Paths
import {
toPosixPath,
sortedPaths,
parentPaths
} from '@bscotch/utility';
toPosixPath("C:\\hello\\world");
const pathList = [
'hello/world',
'hello',
'h/another',
'hello/world/goodbye'
];
sortedPaths(pathList);
parentPaths('/hello/world/foo/bar.txt')
Files
import {
listPathsSync,
listFoldersSync,
listFilesSync,
listFilesByExtensionSync,
removeEmptyDirsSync,
} from '@bscotch/utility';
const recursive = true;
listPathsSync('.',recursive);
listFoldersSync('.',recursive);
listFilesSync('.',recursive);
listFilesByExtensionSync('.','txt',recursive);
listFilesByExtensionSync('.',['txt','md'],recursive);
removeEmptyDirsSync('.');
Waits
import {
waitForMillis,
waitForSeconds,
waitForTick,
} from '@bscotch/utility';
async myAsynFunction(){
await waitForMillis(1000);
await waitForSeconds(1);
await waitForTick();
}
Objects
import {
isPlainObject,
isPlainObjectOrArray,
asObjectIfArray,
flattenObjectPaths,
objectPaths,
getValueAtPath,
setValueAtPath,
objectPathsFromWildcardPath,
transformValueByPath,
} from '@bscotch/utility';
asObjectIfArray(['hello']);
const testObject = {
hello: 'world',
nested: {
layer: 1,
array: [
4,
6,
7
]
}
}
flattenObjectPaths(testObject);
objectPaths(testObject);
getValueAtPath(testObject,'nested.array.2');
setValueAtPath(testObject,'new.0.field',10);
objectPathsFromWildcardPath('nested.*',testObject);
objectPathsFromWildcardPath('nested.array.*',testObject);
transformValueByPath(testObject,'nested.array.*',n=>++n);
Crypto
import {
md5,
sha1,
sha256,
createHash,
encrypt,
decrypt,
} from '@bscotch/utility';
let hash = md5('hello world');
hash = sha256('hello world','base64');
hash = createHash('sha1','hello world');
const key = "00000000000000000000000000000000";
const encrypted = encrypt("Hello World",key);
const sourceAsBuffer = decrypt(encrypted,key);
Dates
import {
dateSort,
dateSortDescending,
dateDifferenceMillis,
dateDifferenceSeconds,
dateDifferenceMinutes,
dateDifferenceHours,
dateDifferenceDays,
dateIsGreaterThan,
dateIsInTheFuture,
dateIsInThePast,
dateIsLessThan,
dateIsOlderThanMillisAgo,
dateIsOlderThanSecondsAgo,
dateIsOlderThanMinutesAgo,
dateIsOlderThanHoursAgo,
dateIsOlderThanDaysAgo,
dateIsValid,
dateAssertIsValid,
} from '@bscotch/utility';
Arrays
import {
arrayTouch,
arrayUntouch,
arrayEachTruthyComparedToLast,
arrayValuesAreDecreasing,
arrayValuesAreIncreasing,
arraySortNumeric,
arraySortNumericDescending,
} from '@bscotch/utility';
arrayTouch( "hello" );
arrayTouch(["hello"]);
arrayTouch( undefined );
arrayValuesAreIncreasing([-10,99,1111]);
arrayUntouch( "hello" );
arrayUntouch(["hello"]);
arrayUntouch(["hello","goodbye"]);