Skip to content

Commit 262b89c

Browse files
committed
60 bytes smaller GZIP size
1 parent c6baa48 commit 262b89c

File tree

1 file changed

+53
-67
lines changed

1 file changed

+53
-67
lines changed

src/stampit.js

Lines changed: 53 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,40 @@
22
'use strict';
33
var _undefined;
44

5-
var var1 = 'roperties';
6-
var var2 = 'ropertyDescriptors';
7-
var var3 = 'static';
8-
var var4 = 'onfiguration';
9-
var _properties = 'p' + var1;
10-
var _deepProperties = 'deepP' + var1;
11-
var _propertyDescriptors = 'p' + var2;
12-
var _staticProperties = var3 + 'P' + var1;
13-
var _staticDeepProperties = var3 + 'DeepP' + var1;
14-
var _staticPropertyDescriptors = var3 + 'P' + var2;
15-
var _configuration = 'c' + var4;
16-
var _deepConfiguration = 'deepC' + var4;
5+
var var1, var2, var3;
6+
var _properties = 'properties';
7+
var _deepProperties = 'deepProperties';
8+
var _propertyDescriptors = 'propertyDescriptors';
9+
var _staticProperties = 'staticProperties';
10+
var _staticDeepProperties = 'staticDeepProperties';
11+
var _staticPropertyDescriptors = 'staticPropertyDescriptors';
12+
var _configuration = 'configuration';
13+
var _deepConfiguration = 'deepConfiguration';
1714
var _deepProps = 'deepProps';
1815
var _deepStatics = 'deepStatics';
1916
var _deepConf = 'deepConf';
2017
var _initializers = 'initializers';
2118
var _methods = 'methods';
2219
var _composers = 'composers';
2320
var _compose = 'compose';
24-
var _object = 'object';
25-
var _length = 'length';
26-
var _create = 'create';
27-
var _Object = Object;
28-
var isArray = Array.isArray;
29-
var defineProperties = _Object.defineProperties;
30-
var defineProperty = _Object.defineProperty;
31-
var getOwnPropertyDescriptor = _Object.getOwnPropertyDescriptor;
32-
var getOwnPropertySymbols = _Object.getOwnPropertySymbols;
33-
var baseStampit = Array.prototype; // temporary reusing the variable
34-
var concat = baseStampit.concat;
35-
var slice = baseStampit.slice;
21+
var baseStampit;
3622

3723
function getOwnPropertyKeys(obj) {
38-
return _Object.getOwnPropertyNames(obj).concat(getOwnPropertySymbols ? getOwnPropertySymbols(obj) : []);
24+
return Object.getOwnPropertyNames(obj).concat(Object.getOwnPropertySymbols ? Object.getOwnPropertySymbols(obj) : []);
3925
}
4026

4127
function _mergeOrAssign(action, dst) {
42-
return slice.call(arguments, 2).reduce(action, dst);
28+
return Array.prototype.slice.call(arguments, 2).reduce(action, dst);
4329
}
4430

4531
function assignOne(dst, src) {
4632
if (src) {
4733
// We need to copy regular props, symbols, getters and setters.
4834
var keys = getOwnPropertyKeys(src), i = 0, desc;
4935
for (; i < keys.length; i += 1) {
50-
desc = getOwnPropertyDescriptor(src, keys[i]);
36+
desc = Object.getOwnPropertyDescriptor(src, keys[i]);
5137
// Make it rewritable because two stamps can have same named getter/setter
52-
defineProperty(dst, keys[i], desc);
38+
Object.defineProperty(dst, keys[i], desc);
5339
}
5440
}
5541
return dst;
@@ -62,12 +48,12 @@
6248
}
6349

6450
function isObject(obj) {
65-
return obj && typeof obj == _object || isFunction(obj);
51+
return obj && typeof obj == 'object' || isFunction(obj);
6652
}
6753

6854
function isPlainObject(value) {
69-
return value && typeof value == _object &&
70-
value.__proto__ == _Object.prototype;
55+
return value && typeof value == 'object' &&
56+
value.__proto__ == Object.prototype;
7157
}
7258

7359
/**
@@ -82,25 +68,25 @@
8268

8369
// According to specification arrays must be concatenated.
8470
// Also, the '.concat' creates a new array instance. Overrides the 'dst'.
85-
if (isArray(src)) return (isArray(dst) ? dst : []).concat(src);
71+
if (Array.isArray(src)) return (Array.isArray(dst) ? dst : []).concat(src);
8672

8773
// Now deal with non plain 'src' object. 'src' overrides 'dst'
8874
// Note that functions are also assigned! We do not deep merge functions.
8975
if (!isPlainObject(src)) return src;
9076

9177
var keys = getOwnPropertyKeys(src), i = 0, key, desc;
92-
for (; i < keys[_length];) {
78+
for (; i < keys.length;) {
9379
key = keys[i++];
94-
desc = getOwnPropertyDescriptor(src, key);
80+
desc = Object.getOwnPropertyDescriptor(src, key);
9581
if (desc.hasOwnProperty('value')) { // is this a regular property?
9682
// Do not merge properties with the 'undefined' value.
9783
if (desc.value !== _undefined) {
9884
// deep merge each property. Recursion!
99-
dst[key] = mergeOne(isPlainObject(dst[key]) || isArray(src[key]) ? dst[key] : {}, src[key]);
85+
dst[key] = mergeOne(isPlainObject(dst[key]) || Array.isArray(src[key]) ? dst[key] : {}, src[key]);
10086
}
10187
} else { // nope, it looks like a getter/setter
10288
// Make it rewritable because two stamps can have same named getter/setter
103-
defineProperty(dst, key, desc);
89+
Object.defineProperty(dst, key, desc);
10490
}
10591
}
10692

@@ -110,10 +96,10 @@
11096
var merge = _mergeOrAssign.bind(0, mergeOne);
11197

11298
function extractUniqueFunctions() {
113-
var1 = concat.apply([], arguments).filter(function(elem, index, array) {
99+
var1 = Array.prototype.concat.apply([], arguments).filter(function(elem, index, array) {
114100
return isFunction(elem) && array.indexOf(elem) === index;
115101
});
116-
return var1[_length] ? var1 : _undefined;
102+
return var1.length ? var1 : _undefined;
117103
}
118104

119105

@@ -141,45 +127,45 @@
141127
* @returns {Descriptor} Standardised descriptor
142128
*/
143129
function standardiseDescriptor(descr) {
144-
var4 = {};
130+
var3 = {};
145131

146-
var4[_methods] = descr[_methods] || _undefined;
132+
var3[_methods] = descr[_methods] || _undefined;
147133

148134
var1 = descr[_properties];
149135
var2 = descr.props;
150-
var4[_properties] = isObject(var1 || var2) ? assign({}, var2, var1) : _undefined;
136+
var3[_properties] = isObject(var1 || var2) ? assign({}, var2, var1) : _undefined;
151137

152-
var4[_initializers] = extractUniqueFunctions(descr.init, descr[_initializers]);
138+
var3[_initializers] = extractUniqueFunctions(descr.init, descr[_initializers]);
153139

154-
var4[_composers] = extractUniqueFunctions(descr[_composers]);
140+
var3[_composers] = extractUniqueFunctions(descr[_composers]);
155141

156142
var1 = descr[_deepProperties];
157143
var2 = descr[_deepProps];
158-
var4[_deepProperties] = isObject(var1 || var2) ? merge({}, var2, var1) : _undefined;
144+
var3[_deepProperties] = isObject(var1 || var2) ? merge({}, var2, var1) : _undefined;
159145

160-
var4[_propertyDescriptors] = descr[_propertyDescriptors];
146+
var3[_propertyDescriptors] = descr[_propertyDescriptors];
161147

162148
var1 = descr[_staticProperties];
163149
var2 = descr.statics;
164-
var4[_staticProperties] = isObject(var1 || var2) ? assign({}, var2, var1) : _undefined;
150+
var3[_staticProperties] = isObject(var1 || var2) ? assign({}, var2, var1) : _undefined;
165151

166152
var1 = descr[_staticDeepProperties];
167153
var2 = descr[_deepStatics];
168-
var4[_staticDeepProperties] = isObject(var1 || var2) ? merge({}, var2, var1) : _undefined;
154+
var3[_staticDeepProperties] = isObject(var1 || var2) ? merge({}, var2, var1) : _undefined;
169155

170156
var1 = descr[_staticPropertyDescriptors];
171157
var2 = descr.name && {name: {value: descr.name}};
172-
var4[_staticPropertyDescriptors] = isObject(var2 || var1) ? assign({}, var1, var2) : _undefined;
158+
var3[_staticPropertyDescriptors] = isObject(var2 || var1) ? assign({}, var1, var2) : _undefined;
173159

174160
var1 = descr[_configuration];
175161
var2 = descr.conf;
176-
var4[_configuration] = isObject(var1 || var2) ? assign({}, var2, var1) : _undefined;
162+
var3[_configuration] = isObject(var1 || var2) ? assign({}, var2, var1) : _undefined;
177163

178164
var1 = descr[_deepConfiguration];
179165
var2 = descr[_deepConf];
180-
var4[_deepConfiguration] = isObject(var1 || var2) ? merge({}, var2, var1) : _undefined;
166+
var3[_deepConfiguration] = isObject(var1 || var2) ? merge({}, var2, var1) : _undefined;
181167

182-
return var4;
168+
return var3;
183169
}
184170

185171
/**
@@ -192,20 +178,20 @@
192178
// Next line was optimized for most JS VMs. Please, be careful here!
193179
var obj = {__proto__: i[_methods]};
194180

195-
var inits = i[_initializers], args = slice.apply(arguments);
181+
var inits = i[_initializers], args = Array.prototype.slice.apply(arguments);
196182
var initializer, returnedValue;
197183

198184
var tmp = i[_deepProperties];
199185
if (tmp) merge(obj, tmp);
200186
tmp = i[_properties];
201187
if (tmp) assign(obj, tmp);
202188
tmp = i[_propertyDescriptors];
203-
if (tmp) defineProperties(obj, tmp);
189+
if (tmp) Object.defineProperties(obj, tmp);
204190

205-
if (!inits || !inits[_length]) return obj;
191+
if (!inits || !inits.length) return obj;
206192

207193
if (options === _undefined) options = {};
208-
for (i = 0; i < inits[_length];) {
194+
for (i = 0; i < inits.length;) {
209195
initializer = inits[i++];
210196
if (isFunction(initializer)) {
211197
returnedValue = initializer.call(obj, options,
@@ -233,7 +219,7 @@
233219
if (var2) assign(var1, var2);
234220

235221
var2 = descriptor[_staticPropertyDescriptors];
236-
if (var2) defineProperties(var1, var2);
222+
if (var2) Object.defineProperties(var1, var2);
237223

238224
var2 = isFunction(var1[_compose]) ? var1[_compose] : compose;
239225
assign(var1[_compose] = function() {
@@ -291,7 +277,7 @@
291277
* @returns {Stamp} A new stamp (aka composable factory function)
292278
*/
293279
function compose() {
294-
var descriptor = concat.apply([this], arguments)
280+
var descriptor = Array.prototype.concat.apply([this], arguments)
295281
.reduce(mergeComposable, {});
296282
return createStamp(descriptor);
297283
}
@@ -367,11 +353,11 @@
367353

368354
function createUtilityFunction(propName, action) {
369355
return function() {
370-
var4 = {};
371-
var4[propName] = action.apply(_undefined, concat.apply([{}], arguments));
356+
var3 = {};
357+
var3[propName] = action.apply(_undefined, Array.prototype.concat.apply([{}], arguments));
372358
var1 = this;
373359

374-
return ((var1 && var1[_compose]) || var2).call(var1, var4);
360+
return ((var1 && var1[_compose]) || var2).call(var1, var3);
375361
};
376362
}
377363

@@ -382,7 +368,7 @@
382368
*/
383369
var2 = allUtilities[_compose] = assign(function stampit() {
384370
var i = 0, composable, composables = [], array = arguments, composerResult = this;
385-
for (; i < array[_length];) {
371+
for (; i < array.length;) {
386372
composable = array[i++];
387373
if (isObject(composable)) {
388374
composables.push(isStamp(composable) ? composable : standardiseDescriptor(composable));
@@ -394,8 +380,8 @@
394380
if (composerResult) composables.unshift(composerResult);
395381

396382
array = composable[_compose][_composers];
397-
if (isArray(array)) {
398-
for (i = 0; i < array[_length];) {
383+
if (Array.isArray(array)) {
384+
for (i = 0; i < array.length;) {
399385
composerResult = array[i++]({stamp: composable, composables: composables});
400386
composable = isStamp(composerResult) ? composerResult : composable;
401387
}
@@ -404,19 +390,19 @@
404390
return composable;
405391
}, allUtilities); // Setting up the shortcut functions
406392

407-
allUtilities[_create] = function() {
393+
allUtilities.create = function() {
408394
return this.apply(_undefined, arguments);
409395
};
410396

411-
var4 = {};
412-
var4[_staticProperties] = allUtilities;
397+
var3 = {};
398+
var3[_staticProperties] = allUtilities;
413399

414400
/**
415401
* Infected stamp. Used as a storage of the infection metadata
416402
* @type {Function}
417403
* @return {Stamp}
418404
*/
419-
baseStampit = compose(var4);
405+
baseStampit = compose(var3);
420406

421407
var2[_compose] = var2.bind(); // bind to undefined
422408
var2.version = 'VERSION';

0 commit comments

Comments
 (0)