-
Notifications
You must be signed in to change notification settings - Fork 5
/
lodash-natives.js
59 lines (50 loc) · 1.47 KB
/
lodash-natives.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
/**
* Native alternatives for lodash.
*
* @comment disabling eslint rules so examples stay identical to github
*
* @see {@link https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore}
* @see {@link https://xebia.com/blog/you-might-not-need-lodash-in-your-es2015-project/}
*/
/* eslint-disable func-names */
/* eslint-disable no-prototype-builtins */
/* eslint-disable no-param-reassign */
/* eslint-disable unicorn/prevent-abbreviations */
/* eslint-disable unicorn/no-reduce */
const capitalize = function (string) {
if (typeof string !== 'string') {
throw new TypeError("The 'string' argument for _capitalize() must be a string");
}
return string.charAt(0).toUpperCase() + string.slice(1);
};
const pick = function (object, keys) {
return keys.reduce((obj, key) => {
if (object && object.hasOwnProperty(key)) {
obj[key] = object[key];
}
return obj;
}, {});
};
// ---------------------------------------------------
// omit alternative taken from
// https://dustinpfister.github.io/2019/08/19/lodash_omit/
// ---------------------------------------------------
const inProps = function (key, props) {
return props.some((omitKey) => {
return omitKey === key;
});
};
const omit = function (object, properties) {
const newObject = {};
Object.keys(object).forEach((key) => {
if (!inProps(key, properties)) {
newObject[key] = object[key];
}
});
return newObject;
};
module.exports = {
capitalize,
omit,
pick,
};