1 | const merge = require('deepmerge');
|
2 | const Chainable = require('./Chainable');
|
3 |
|
4 | module.exports = class extends Chainable {
|
5 | constructor(parent) {
|
6 | super(parent);
|
7 | this.store = new Map();
|
8 | }
|
9 |
|
10 | extend(methods) {
|
11 | this.shorthands = methods;
|
12 | methods.forEach(method => {
|
13 | this[method] = value => this.set(method, value);
|
14 | });
|
15 | return this;
|
16 | }
|
17 |
|
18 | clear() {
|
19 | this.store.clear();
|
20 | return this;
|
21 | }
|
22 |
|
23 | delete(key) {
|
24 | this.store.delete(key);
|
25 | return this;
|
26 | }
|
27 |
|
28 | order() {
|
29 | const entries = [...this.store].reduce((acc, [key, value]) => {
|
30 | acc[key] = value;
|
31 | return acc;
|
32 | }, {});
|
33 | const names = Object.keys(entries);
|
34 | const order = [...names];
|
35 |
|
36 | names.forEach(name => {
|
37 | if (!entries[name]) {
|
38 | return;
|
39 | }
|
40 |
|
41 | const { __before, __after } = entries[name];
|
42 |
|
43 | if (__before && order.includes(__before)) {
|
44 | order.splice(order.indexOf(name), 1);
|
45 | order.splice(order.indexOf(__before), 0, name);
|
46 | } else if (__after && order.includes(__after)) {
|
47 | order.splice(order.indexOf(name), 1);
|
48 | order.splice(order.indexOf(__after) + 1, 0, name);
|
49 | }
|
50 | });
|
51 |
|
52 | return { entries, order };
|
53 | }
|
54 |
|
55 | entries() {
|
56 | const { entries, order } = this.order();
|
57 |
|
58 | if (order.length) {
|
59 | return entries;
|
60 | }
|
61 |
|
62 | return undefined;
|
63 | }
|
64 |
|
65 | values() {
|
66 | const { entries, order } = this.order();
|
67 |
|
68 | return order.map(name => entries[name]);
|
69 | }
|
70 |
|
71 | get(key) {
|
72 | return this.store.get(key);
|
73 | }
|
74 |
|
75 | getOrCompute(key, fn) {
|
76 | if (!this.has(key)) {
|
77 | this.set(key, fn());
|
78 | }
|
79 | return this.get(key);
|
80 | }
|
81 |
|
82 | has(key) {
|
83 | return this.store.has(key);
|
84 | }
|
85 |
|
86 | set(key, value) {
|
87 | this.store.set(key, value);
|
88 | return this;
|
89 | }
|
90 |
|
91 | merge(obj, omit = []) {
|
92 | Object.keys(obj).forEach(key => {
|
93 | if (omit.includes(key)) {
|
94 | return;
|
95 | }
|
96 |
|
97 | const value = obj[key];
|
98 |
|
99 | if (
|
100 | (!Array.isArray(value) && typeof value !== 'object') ||
|
101 | value === null ||
|
102 | !this.has(key)
|
103 | ) {
|
104 | this.set(key, value);
|
105 | } else {
|
106 | this.set(key, merge(this.get(key), value));
|
107 | }
|
108 | });
|
109 |
|
110 | return this;
|
111 | }
|
112 |
|
113 | clean(obj) {
|
114 | return Object.keys(obj).reduce((acc, key) => {
|
115 | const value = obj[key];
|
116 |
|
117 | if (value === undefined) {
|
118 | return acc;
|
119 | }
|
120 |
|
121 | if (Array.isArray(value) && !value.length) {
|
122 | return acc;
|
123 | }
|
124 |
|
125 | if (
|
126 | Object.prototype.toString.call(value) === '[object Object]' &&
|
127 | !Object.keys(value).length
|
128 | ) {
|
129 | return acc;
|
130 | }
|
131 |
|
132 | acc[key] = value;
|
133 |
|
134 | return acc;
|
135 | }, {});
|
136 | }
|
137 |
|
138 | when(
|
139 | condition,
|
140 | whenTruthy = Function.prototype,
|
141 | whenFalsy = Function.prototype
|
142 | ) {
|
143 | if (condition) {
|
144 | whenTruthy(this);
|
145 | } else {
|
146 | whenFalsy(this);
|
147 | }
|
148 |
|
149 | return this;
|
150 | }
|
151 | };
|