-
Notifications
You must be signed in to change notification settings - Fork 1
/
general.js
65 lines (50 loc) · 1.48 KB
/
general.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { produce } from "immer";
import {
complement,
curry,
curryN,
either,
equals,
isEmpty,
isNil,
} from "ramda";
/**
* @template {Function} T
* @param {T} func
* @returns {T}
*/
export const nullSafe = func =>
// @ts-ignore
curryN(func.length, (...args) => {
const dataArg = args[func.length - 1];
return isNil(dataArg) ? dataArg : func(...args);
});
export const noop = () => {};
export const toLabelAndValue = string => ({ label: string, value: string });
// eslint-disable-next-line default-param-last
export const getRandomInt = (a = Number.MAX_SAFE_INTEGER, b) => {
if (b) {
a = Math.ceil(a);
b = Math.floor(b);
} else {
b = a;
a = 0;
}
return Math.floor(Math.random() * (b - a) + a);
};
export const randomPick = (...args) => {
const randomNumber = getRandomInt(0, args.length);
return args[randomNumber];
};
export const dynamicArray = (count, elementGenerator) =>
Array.from({ length: count }, (_, index) => elementGenerator(index));
export const isNotEmpty = /*#__PURE__*/ complement(isEmpty);
export const notEquals = /*#__PURE__*/ curry((x, y) => x !== y);
export const isNot = notEquals;
export const isNotPresent = /*#__PURE__*/ either(isNil, isEmpty);
export const isPresent = /*#__PURE__*/ complement(isNotPresent);
export const notEqualsDeep = /*#__PURE__*/ complement(equals);
export const isNotEqualDeep = notEqualsDeep;
export const modifyWithImmer = /*#__PURE__*/ curry((modifier, data) =>
produce(data, modifier)
);