Following are JavaScript libraries that help you write more concise and maintainable code, often combined with functional programming paradigm. When integrating, cherry-pick must be taken to include in the bundle only the used functions.
They provide a huge collection of different methods, often serving completely separate purposes. Some can easily prove to be useful and make your code less bloated, while others can simply be wrappers around native functionalities provided for convenience.
import _ from 'lodash';
_.filter([1, 2, 3, 4, 5], (num) => num % 2 === 0); //=> [2, 4]
import _ from 'underscore';
_.map([1, 2, 3], n => n * 2); //=> [2, 4, 6]
_.reduce([1, 2, 3], (sum, n) => sum + n, 0); //=> 6
import * as R from 'ramda';
R.add(2, 3); //=> 5
R.add(7)(10); //=> 17
import {derivative, evaluate, pow, sqrt} from 'mathjs'
sqrt(-4) //=> 2i
pow([[-1, 2], [3, 1]], 2) //=> [[7, 0], [0, 7]]
derivative('x^2 + x', 'x') //=> 2 * x + 1
evaluate('1.2 * (2 + 4.5)') //=> 7.8
import { format, formatDistance, formatRelative, subDays } from 'date-fns'
format(new Date(), "'Today is a' eeee")
//=> "Today is a Friday"
formatDistance(subDays(new Date(), 3), new Date(), { addSuffix: true })
//=> "3 days ago"
formatRelative(subDays(new Date(), 3), new Date())
//=> "last Friday at 7:26 p.m."
import dayjs from 'dayjs'
dayjs('2019-01-25').format('DD/MM/YYYY') //=> '25/01/2019'
import voca from 'voca';
voca.kebabCase('goodbye blue sky'); //=> 'goodbye-blue-sky'
<!-- Target -->
<input id="foo" value="https://byby.dev">
<!-- Trigger -->
<button class="btn" data-clipboard-target="#foo">
<img src="assets/clippy.svg" alt="Copy to clipboard">
</button>
import { produce } from "immer";
const baseState = [
{
title: "Learn TypeScript",
done: true,
},
{
title: "Try Immer",
done: false,
},
];
const nextState = produce(baseState, (draftState) => {
draftState.push({ title: "Tweet about it" });
draftState[1].done = true;
});
/* =>
[
{ title: 'Learn TypeScript', done: true },
{ title: 'Try Immer', done: true },
{ title: 'Tweet about it' }
]
*/
A-Za-z0-9_-
), so ID size was reduced from 36 to 21 symbols.import { nanoid } from 'nanoid'
model.id = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"