Skip to content

Releases: LeadFisherSolutions/leadutils

v1.2.0

02 Jun 19:14
Compare
Choose a tag to compare

1.2.0 - 2023-06-102

  • move from src to lib
  • package.json files

v1.1.0

14 May 14:59
Compare
Choose a tag to compare

#1.1.0 - 2023-05-14

  • CHANGELOG.md
  • CONTRIBUTING.md
  • .npmignore

v1.0.0

13 May 19:59
Compare
Choose a tag to compare

[Node js] Common.js | Universal task scripts

Usage

const utils = require('leadutils');
// const { time, obj, string, time, net, crypto, utils, oop, fp, pp, async } = utils;
console.log(utils.time.prettify('h:m:s / D.M|Y', new Date())); // 18:50:54 / 07.05|2023

Modules

Time module | .time
  • (prettify) Return date in any string format
prettify(format: string, date?: Date | string | number) => string;
// format is any combination of letters [YMDhmsi] with any optional separators
// Y - Year; M - Month; D - Day; h - Hours; m - Minute; s - Second; i - Millisecond
const utils = require('leadutils');
utils.time.prettify('h:m:s / D.M|Y', new Date()); // 18:50:54 / 07.05|2023
utils.time.prettify('h-m-s.i', new Date()); // 18-50-54.045
  • (duration) Return duration in ms from string
function duration(time: string): number;
// time is any combination of letters [dhms] with values
// d - Day; h - Hours; m - Minute; s - Second;
// Example: 1d 1h 1m 1s 90061000
const utils = require('leadutils');
utils.time.duration('5s'); // 5000
utils.time.duration('24h'); // 86400000
utils.time.duration('1d'); // 86400000
utils.time.duration('1d 1h 1m 5s'); // 90065000
  • (compare) Create any dates compare functions
const utils = require('leadutils');
utils.time.compare((a, b) => a > b)('2023-05-07', '2023-05-08'); // false
utils.time.compare((a, b) => a > b)('2023-01-01', '2021-05-08'); // true
  • (formatDuration) Make seconds in more readable format
const utils = require('leadutils');
utils.time.formatDuration(1000); // 1 second
utils.time.formatDuration(60000); // 1 minute
utils.time.formatDuration(60001); // 1 minute 1 second
utils.time.formatDuration(90000); // 1 minute 30 seconds
  • (divideDuration) Get a divided ms by all day time measurements object
const utils = require('leadutils');
utils.time.divideDuration(new Date()); // { day: 19484, hour: 16, minute: 6, second: 34, millisecond: 818 }
utils.time.divideDuration(90020); // { day: 0, hour: 0, minute: 1, second: 30, millisecond: 20 }
  • (datesDiff) Return difference between two dates in any time measurement
const utils = require('leadutils');
let tomorrow = new Date(new Date().setDate(new Date().getDate() + 1));
utils.time.datesDiff(new Date(), tomorrow); // 1
utils.time.datesDiff(new Date(), tomorrow, 'hour'); // 24

Parallel programming module | .pp

(Semaphore) for limit concurrency accessing limited resource

class Semaphore(concurrency: number, size?: number, timeout?: number){
  empty: boolean;
  enter: Promise<boolean>;
  leave: void;
}

Different utils module | .utils
  • (shuffle) Shuffle array
const utils = require('leadutils');
utils.array.shuffle([1, 2, 3]); // ~[2,3,1]
  • (sample) Get sample from array
const utils = require('leadutils');
utils.array.sample([1, 2, 3]); // ~2
utils.array.sample([1, 2, 3]); // ~3
utils.array.sample([1, 2, 3]); // ~2
  • (range) Creates array from range
const utils = require('leadutils');
utils.array.range(4, 7); // [4,5,6,7]
  • (random) Generate a random number
const utils = require('leadutils');
utils.utils.random(0, 5); // 2
utils.utils.random(0, 5); // 4
  • (equals) Deep equals for any type of values
const utils = require('leadutils');
utils.utils.equals(0, 0); // true
utils.utils.equals('test', 'test'); // true
utils.utils.equals([1, 2], [1, 2]); // true
utils.utils.equals([1, 2], [1, 2, 3]); // false
utils.utils.equals({ a: true, b: { foo: true } }, { a: true, b: { foo: true } }); // true
utils.utils.equals({ a: true, b: { foo: true } }, { a: true, b: { foo: true, bar: true } }); // false
  • (prettyBytes) Return more readable bytes format
const utils = require('leadutils');
utils.utils.prettyBytes(10000000); // 10 MB
utils.utils.prettyBytes(1); // 1 B
  • (mostPerformant) Select fastest method
const utils = require('leadutils');
utils.utils.mostPerformant([() => 1 + 2, () => 1 * 2, () => 1 / 2, () => 1 - 2]); // ~0
  • (timeTaken) Log execution time
const utils = require('leadutils');
utils.utils.timeTaken((a, b) => a + b)(2 + 3); // return 5; log => timeTaken: 0.297ms

Async programming module | .async
function delay(msec: number, signal?: EventEmitter): Promise<void>;
function timeout(msec: number, signal?: EventEmitter): Promise<void>;
  • (delay) Promise resolve after provided msec, with abort controller
const utils = require('leadutils');
utils.async.delay('10s').then(() => console.log('after delay'));
  • (timeout) Promise resolve if signal was passed in provided ms, else reject
function delay(msec: number, signal?: EventEmitter): Promise<void>;
const utils = require('leadutils');
utils.async.delay('10s').then(() => console.log('after delay'));

Network utils module | .net
  • (receiveBody) Stream body receiver
function receiveBody(stream: IncomingMessage): Promise<Buffer | null>;
  • (createXML) Create XML response
const utils = require('leadutils');
const xmlBody = utils.net
  .createXML()
  .add({ loc: 'https://leadfisher.ru/', priority: 1, time: '2022-04-12' })
  .add({ loc: 'https://leadfisher.ru/test', priority: 0.4, time: '2022-04-10' }).get;
// <?xml version="1.0" encoding="UTF-8"..
  • (intIP) Get ip integer interpretation
const utils = require('leadutils');
utils.net.intIP('127.0.0.1'); // 2130706433
  • (parseCookie) Parse cookie from string
const utils = require('leadutils');
let cookie = 'test=123;';
utils.net.parseCookie(cookie); // {test: 123}
  • (removePort) Parse cookie from string
const utils = require('leadutils');
utils.net.removePort('https://leadfisher.ru/api/test'); // https://leadfisher.ru

String manipulations utils | .string
  • (escape, escapeHTML, unescapeHTML) Escape unfriendly characters
const utils = require('leadutils');
utils.string.escape('https://leadfisher.ru'); // 'https:\\/\\/leadfisher\\.ru'
utils.string.escapeHTML('<script>alert("leadfisher !");</script>'); // &lt;script&gt;alert(&quot;leadfisher !&quot;);&lt;script&gt;
utils.string.unescapeHTML('&lt;script&gt;alert(&quot;leadfisher !&quot;);&lt;script&gt;'); // <script>alert("leadfisher !");</script>
  • (template) String templates
const utils = require('leadutils');
const template = utils.string.template`Hello ${'put_here'} !`;
template({ put_here: 'Alex' }); // Hello Alex !
template({ put_here: 'Admin' }); // Hello Admin !
  • (yesNo) Parse user answers
const utils = require('leadutils');
utils.string.yesNo('yes'); // true
utils.string.yesNo('y'); // true
utils.string.yesNo('n'); // false
utils.string.yesNo(''); // false
  • (words) Parse all words from string
const utils = require('leadutils');
utils.string.words('Hello word!'); // ['Hello', 'word']
  • (reverse) String or Array reverse
const utils = require('leadutils');
utils.string.reverse('Hello word!'); // !word olleH
utils.string.reverse([1, 2, 3]); // 321
  • (phonePrettify, phonePurify, normalizeEmail) Phone manipulations utils
const utils = require('leadutils');
utils.string.phonePrettify('79999999999'); // +7 (999) 999-99-99
utils.string.phonePurify('+7 (999) 999-99-99'); // 79999999999
utils.string.normalizeEmail('Test@MAIL.domain', 'Test@mail.domain'); // 321
  • (fileExt, fileName) System path manipulation utils
const utils = require('leadutils');
utils.string.fileExt('/home/user/index.js'); // js
utils.string.fileName('/home/user/index.js'); // index
  • (toString, fromString, isValidJSON, jsonParse) String parsers

jsonParse and fromString are both safe parsers

const utils = require('leadutils');
utils.string.toString(1); // '1'
utils.string.toString(true); // 'true'
utils.string.toString(undefined); // 'undefined'
utils.string.fromString('test'); // 'test'
utils.string.toString({ foo: { boo: true } }); // '{"foo": { "boo": true }}'
utils.string.fromString('1'); // 1
utils.string.fromString('true'); // true
utils.string.fromString('undefined'); // undefined
utils.string.fromString('test'); // 'test'
utils.string.fromString('{"foo": { "boo": true }}'); // { foo: { boo: true } }
utils.string.isValidJSON('{}'); // true
utils.string.jsonParse('{}'); // {}
utils.string.jsonParse(''); // null
utils.string.jsonParse(123); // null
utils.string.isValidJSON({}); // false

Objects manipulation utils | .obj
  • (deepClone) Clone object with all nested objects, no reference copies
const utils = require('leadutils');
const test = { foo: { boo: { bar: 2 } } }...
Read more