@pleisto/active-support
A TypeScript utility library designed to optimize for programmer happiness.
NOTES: If you want to add a new methods, make sure it will be used on both the client and the server side.
Any methods that will only used in a browser or NodeJS environment should not be added here.
Features
Alternative to lodash
You could directly use @pleisto/active-support
instead of lodash
and lodash-es
.
import { differenceBy, zip, isString } from '@pleisto/active-support'
If some of the methods in lodash do not exist in @pleisto/active-support
, it is because
the are natively supported in modern ECMAScript. see YOU MIGHT NOT NEED LODASH for more information.
Unit Converter
Millisecond conversion
import { ms } from '@pleisto/active-support';
ms('2 days')
ms('2.5 h')
ms('-3 days) // -259200000
ms('-200') // -200
ms(60000) // '1m'
ms(ms('10 hours))
ms(6000, { long: true })
ms(2*6000, { long: true })
see vercel/ms for more information.
ByteSize conversion
import { byteSize } from '@pleisto/active-support'
byteSize('3 mb')
byteSize('2 Gigabytes')
byteSize(32_000_000)
NOTICE:
We use base 10 instead of base 2 for bit.
See IEC 60027-2 A.2 and ISO/IEC 80000 for more information.
Type Checking Utilities
You could use most of the type checking utilities in lodash directly, such as isString
, isEmpty
and isBuffer
.
In addition we support methods such as isUUID
nad isBlack
. See src/isType.ts
for more information.
isBlack
isBlack
method could be used to check if any value is empty or undefined/null, just as it does in Ruby on Rails.
Inflections
import { pluralize, singularize } from '@pleisto/active-support'
pluralize('word')
pluralize('datum')
singularize('quizzes')
singularize('news')
singularize('are')
Rust style error handling
import { ok, err } from '@pleisto/active-support'
const yesss = ok(someAesomeValue)
const mappedYes = yesss.map(doingSuperUsefulStuff)
if (mappedYes.isOk()) {
doStuffWith(mappedYes.value)
} else {
doStuffWith(mappedYes.error)
}
See neverthrow for more information.
Rust style pattern matching
import { match, P } from '@pleisto/active-support'
type Data =
| { type: 'text'; content: string }
| { type: 'img'; src: string };
type Result =
| { type: 'ok'; data: Data }
| { type: 'error'; error: Error };
const result: Result = ...;
return match(result)
.with({ type: 'error' }, () => `<p>Oups! An error occured</p>`)
.with({ type: 'ok', data: { type: 'text' } }, (res) => `<p>${res.data.content}</p>`)
.with({ type: 'ok', data: { type: 'img', src: P.select() } }, (src) => `<img src=${src} />`)
.exhaustive();
See ts-pattern for more information.
Utilities
array2Tree
Converts an array of items with ids and parent ids to a nested tree in a performant way (time complexity O(n)
).
Se Performant array to tree for more information.
equals
The fastest deep equal with ES6 Map, Set and Typed arrays support.
Based on fast-deep-equal/es6/react