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');
Strings
import {
undent,
oneline
} 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.
`;
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 {
resolveInMillis,
resolveInSeconds,
resolveInNextTick,
} from '@bscotch/utility';
async myAsynFunction(){
await resolveInMillis(1000);
await resolveInSeconds(1);
await resolveInNextTick();
}
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,
} from '@bscotch/utility';
let hash = md5('hello world');
hash = sha256('hello world','base64');
hash = createHash('sha1','hello world');
Dates
import {
isValidDate,
dateIsOlderThanSecondsAgo,
dateIsOlderThanMinutesAgo,
dateIsOlderThanHoursAgo,
dateIsOlderThanDaysAgo,
dateIsInTheFuture,
dateIsInThePast,
dateIsGreaterThan,
dateIsLessThan,
chronologySortReverse,
chronologySort
} from '@bscotch/utility';