Skip to content

Commit f99b2c0

Browse files
authored
Add browser-support (#75)
1 parent a8244cf commit f99b2c0

File tree

5 files changed

+449
-337
lines changed

5 files changed

+449
-337
lines changed

lib/JSONAPISerializer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require('setimmediate');
2+
13
const {
24
pick,
35
isEmpty,

lib/helpers.js

Lines changed: 77 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
1-
const {
2-
pick,
3-
isEmpty,
4-
omit,
5-
isPlainObject,
6-
isObjectLike,
7-
transform,
8-
toKebabCase,
9-
toSnakeCase,
10-
toCamelCase,
11-
} = require('30-seconds-of-code');
12-
const set = require('lodash.set');
1+
/* eslint-disable no-sequences */
2+
/* eslint-disable no-return-assign */
3+
134
const LRU = require('./lru-cache');
145

156
// https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore#_get
@@ -21,6 +12,80 @@ const get = (obj, path, defaultValue) => {
2112
return result === undefined || result === obj ? defaultValue : result;
2213
};
2314

15+
// https://stackoverflow.com/questions/54733539/javascript-implementation-of-lodash-set-method
16+
const set = (obj, path, value) => {
17+
if (Object(obj) !== obj) return obj; // When obj is not an object
18+
// If not yet an array, get the keys from the string-path
19+
if (!Array.isArray(path)) path = path.toString().match(/[^.[\]]+/g) || [];
20+
path.slice(0, -1).reduce(
21+
(
22+
a,
23+
c,
24+
i // Iterate all of them except the last one
25+
) =>
26+
Object(a[c]) === a[c] // Does the key exist and is its value an object?
27+
? // Yes: then follow that path
28+
a[c]
29+
: // No: create the key. Is the next key a potential array-index?
30+
(a[c] =
31+
// eslint-disable-next-line no-bitwise
32+
Math.abs(path[i + 1]) >> 0 === +path[i + 1]
33+
? [] // Yes: assign a new array object
34+
: {}), // No: assign a new plain object
35+
obj
36+
)[path[path.length - 1]] = value; // Finally assign the value to the last key
37+
return obj; // Return the top-level object to allow chaining
38+
};
39+
40+
// https://github.com/30-seconds/30-seconds-of-code/blob/master/snippets/pick.md
41+
const pick = (obj, arr) =>
42+
arr.reduce((acc, curr) => (curr in obj && (acc[curr] = obj[curr]), acc), {});
43+
44+
// https://github.com/30-seconds/30-seconds-of-code/blob/master/snippets/isEmpty.md
45+
const isEmpty = (val) => val == null || !(Object.keys(val) || val).length;
46+
47+
// https://github.com/30-seconds/30-seconds-of-code/blob/master/snippets/omit.md
48+
const omit = (obj, arr) =>
49+
Object.keys(obj)
50+
.filter((k) => !arr.includes(k))
51+
.reduce((acc, key) => ((acc[key] = obj[key]), acc), {});
52+
53+
// https://github.com/30-seconds/30-seconds-of-code/blob/master/snippets/isObjectLike.md
54+
const isObjectLike = (val) => val !== null && typeof val === 'object';
55+
56+
// https://github.com/30-seconds/30-seconds-of-code/blob/master/snippets/isPlainObject.md
57+
const isPlainObject = (val) => !!val && typeof val === 'object' && val.constructor === Object;
58+
59+
// https://github.com/30-seconds/30-seconds-of-code/blob/master/snippets/transform.md
60+
const transform = (obj, fn, acc) => Object.keys(obj).reduce((a, k) => fn(a, obj[k], k, obj), acc);
61+
62+
// https://github.com/30-seconds/30-seconds-of-code/blob/master/snippets/toKebabCase.md
63+
const toKebabCase = (str) =>
64+
str &&
65+
str
66+
.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
67+
.map((x) => x.toLowerCase())
68+
.join('-');
69+
70+
// https://github.com/30-seconds/30-seconds-of-code/blob/master/snippets/toSnakeCase.md
71+
const toSnakeCase = (str) =>
72+
str &&
73+
str
74+
.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
75+
.map((x) => x.toLowerCase())
76+
.join('_');
77+
78+
// https://github.com/30-seconds/30-seconds-of-code/blob/master/snippets/toCamelCase.md
79+
const toCamelCase = (str) => {
80+
const s =
81+
str &&
82+
str
83+
.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
84+
.map((x) => x.slice(0, 1).toUpperCase() + x.slice(1).toLowerCase())
85+
.join('');
86+
return s.slice(0, 1).toLowerCase() + s.slice(1);
87+
};
88+
2489
module.exports = {
2590
get,
2691
set,

0 commit comments

Comments
 (0)