Skip to content

Commit 5c45ca9

Browse files
committed
simplify array of function merging
1 parent 5853b2a commit 5c45ca9

File tree

1 file changed

+39
-48
lines changed

1 file changed

+39
-48
lines changed

stampit.js

Lines changed: 39 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,27 @@ export default (function () {
33
// 1. The minified JS file becomes 20% smaller.
44
// 2. The minified GZIP file becomes 10% smaller.
55

6+
function isFunction(obj) {
7+
return typeof obj === "function";
8+
}
9+
10+
function isObject(obj) {
11+
return (obj && typeof obj === "object") || isFunction(obj);
12+
}
13+
14+
function isPlainObject(value) {
15+
return value && typeof value === "object" && value.__proto__ === Object.prototype;
16+
}
17+
18+
/**
19+
* Returns true if argument is a stamp.
20+
* @param {*} obj Any object
21+
* @returns {Boolean} True is the obj is a stamp
22+
*/
23+
function isStamp(obj) {
24+
return isFunction(obj) && isFunction(obj.compose);
25+
}
26+
627
function getOwnPropertyKeys(obj) {
728
return [...Object.getOwnPropertyNames(obj), ...Object.getOwnPropertySymbols(obj)];
829
}
@@ -25,27 +46,6 @@ export default (function () {
2546
return dst;
2647
}
2748

28-
function isFunction(obj) {
29-
return typeof obj === "function";
30-
}
31-
32-
function isObject(obj) {
33-
return (obj && typeof obj === "object") || isFunction(obj);
34-
}
35-
36-
function isPlainObject(value) {
37-
return value && typeof value === "object" && value.__proto__ === Object.prototype;
38-
}
39-
40-
/**
41-
* Returns true if argument is a stamp.
42-
* @param {*} obj Any object
43-
* @returns {Boolean} True is the obj is a stamp
44-
*/
45-
function isStamp(obj) {
46-
return isFunction(obj) && isFunction(obj.compose);
47-
}
48-
4949
/**
5050
* Unlike _.merge(), our merge() copies symbols, getters and setters.
5151
* The 'src' argument plays the command role.
@@ -61,7 +61,7 @@ export default (function () {
6161
// Create a new array instance. Overrides the 'dst'.
6262
if (Array.isArray(src)) {
6363
if (Array.isArray(dst)) return [...dst, ...src];
64-
return [...src]; // ignore the 'dst'
64+
return [...src]; // ignore the 'dst', clone the src
6565
}
6666
// Now deal with non plain 'src' object. 'src' overrides 'dst'
6767
// Note that functions are also assigned! We do not deep merge functions.
@@ -91,17 +91,8 @@ export default (function () {
9191
const merge = (dst, ...args) => args.reduce(mergeOne, dst);
9292

9393
function extractUniqueFunctions(...args) {
94-
const funcs = new Set();
95-
for (const arg of args) {
96-
if (Array.isArray(arg)) {
97-
for (const f of arg) {
98-
if (isFunction(f)) funcs.add(f);
99-
}
100-
} else if (isFunction(arg)) {
101-
funcs.add(arg);
102-
}
103-
}
104-
return funcs.size ? Array.from(funcs) : undefined;
94+
const funcs = new Set(args.flat().filter(isFunction));
95+
return funcs.size ? [...funcs] : undefined;
10596
}
10697

10798
/**
@@ -117,8 +108,8 @@ export default (function () {
117108
// The instance of this stamp
118109
let instance = descriptor.methods ? Object.create(descriptor.methods) : {};
119110

120-
if (descriptor.deepProperties) merge(instance, descriptor.deepProperties);
121-
if (descriptor.properties) assign(instance, descriptor.properties);
111+
mergeOne(instance, descriptor.deepProperties);
112+
assignOne(instance, descriptor.properties);
122113
if (descriptor.propertyDescriptors) Object.defineProperties(instance, descriptor.propertyDescriptors);
123114

124115
const inits = descriptor.initializers;
@@ -166,15 +157,15 @@ export default (function () {
166157

167158
srcComposable = srcComposable?.compose || srcComposable;
168159
if (isObject(srcComposable)) {
169-
mergeAssign("methods", assign);
170-
mergeAssign("properties", assign);
171-
mergeAssign("deepProperties", merge);
172-
mergeAssign("propertyDescriptors", assign);
173-
mergeAssign("staticProperties", assign);
174-
mergeAssign("staticDeepProperties", merge);
175-
mergeAssign("staticPropertyDescriptors", assign);
176-
mergeAssign("configuration", assign);
177-
mergeAssign("deepConfiguration", merge);
160+
mergeAssign("methods", assignOne);
161+
mergeAssign("properties", assignOne);
162+
mergeAssign("deepProperties", mergeOne);
163+
mergeAssign("propertyDescriptors", assignOne);
164+
mergeAssign("staticProperties", assignOne);
165+
mergeAssign("staticDeepProperties", mergeOne);
166+
mergeAssign("staticPropertyDescriptors", assignOne);
167+
mergeAssign("configuration", assignOne);
168+
mergeAssign("deepConfiguration", mergeOne);
178169
concatAssignFunctions("initializers");
179170
concatAssignFunctions("composers");
180171
}
@@ -197,16 +188,16 @@ export default (function () {
197188
let stamp = createEmptyStamp();
198189
const descriptor = composables.reduce(mergeComposable, {});
199190

200-
merge(stamp, descriptor.staticDeepProperties);
201-
assign(stamp, descriptor.staticProperties);
191+
mergeOne(stamp, descriptor.staticDeepProperties);
192+
assignOne(stamp, descriptor.staticProperties);
202193
if (descriptor.staticPropertyDescriptors) Object.defineProperties(stamp, descriptor.staticPropertyDescriptors);
203194

204195
const c = isFunction(stamp.compose) ? stamp.compose : compose;
205196
stamp.compose = function (...args) {
206197
return c(this, ...args);
207198
};
208199

209-
assign(stamp.compose, descriptor);
200+
assignOne(stamp.compose, descriptor);
210201

211202
const composers = stamp.compose.composers;
212203
if (Array.isArray(composers)) {
@@ -387,7 +378,7 @@ export default (function () {
387378
return compose(this, { staticProperties: allUtilities }, ...args.map(standardiseDescriptor));
388379
}
389380

390-
assign(stampit, allUtilities); // Setting up the shortcut functions
381+
assignOne(stampit, allUtilities); // Setting up the shortcut functions
391382

392383
stampit.compose = stampit.bind(); // bind to undefined
393384
stampit.version = "VERSION"; // This will be replaced at the build time with the proper version taken from the package.json

0 commit comments

Comments
 (0)