Skip to content

Commit

Permalink
Add support for getters/setters in any of the metadata.
Browse files Browse the repository at this point in the history
Drop the freaking Babel.
  • Loading branch information
koresar committed Jun 21, 2019
1 parent 11b1aaa commit c1a9a72
Show file tree
Hide file tree
Showing 22 changed files with 93 additions and 95 deletions.
9 changes: 3 additions & 6 deletions package.json
Expand Up @@ -28,12 +28,9 @@
},
"dependencies": {},
"devDependencies": {
"babel-cli": "^6.10.1",
"babel-preset-env": "^1.6.0",
"babelify": "^7.3.0",
"benchmark": "^2.1.1",
"browserify": "^14.4.0",
"check-compose": "^4.0.0",
"check-compose": "^5.0.0",
"dependency-check": "^2.5.0",
"eslint": "^3.7.0",
"eslint-config-airbnb-base": "^11.0.0",
Expand All @@ -53,9 +50,9 @@
"scripts": {
"cov": "npm run cov:clean && npm run cov:generate",
"cov:clean": "rimraf ./coverage/",
"cov:generate": "babel-node --presets=env ./node_modules/.bin/istanbul cover test",
"cov:generate": "istanbul cover test",
"pretest": "npm run build",
"test": "babel-node --presets=env test && npm run lint",
"test": "node test && npm run lint",
"posttest": "node test/benchmark",
"browsertest": "mkdirp ./dist/ && browserify ./test/index.js -t [ babelify --presets [ env ] ] > dist/test_bundle.js && cp ./test/index.html ./dist/ && open ./dist/index.html",
"clean": "rimraf dist/*",
Expand Down
42 changes: 27 additions & 15 deletions src/stampit.js
Expand Up @@ -27,26 +27,36 @@
var _Object = Object;
var isArray = Array.isArray;
var defineProperties = _Object.defineProperties;
var objectKeys = _Object.keys;
var defineProperty = _Object.defineProperty;
var getOwnPropertyDescriptor = _Object.getOwnPropertyDescriptor;
var getOwnPropertySymbols = _Object.getOwnPropertySymbols;
var baseStampit = Array.prototype; // temporary reusing the variable
var concat = baseStampit.concat;
var slice = baseStampit.slice;

function getOwnPropertyKeys(obj) {
return _Object.getOwnPropertyNames(obj).concat(getOwnPropertySymbols ? getOwnPropertySymbols(obj) : []);
}

function _mergeOrAssign(action, dst) {
return slice.call(arguments, 2).reduce(action, dst);
}

function assignOne(dst, src) {
if (src) {
var keys = objectKeys(src);
for (var i = 0; i < keys[_length]; i++) {
dst[keys[i]] = src[keys[i]];
// We need to copy regular props, symbols, getters and setters.
var keys = getOwnPropertyKeys(src), i = 0, desc;
for (; i < keys.length; i += 1) {
desc = getOwnPropertyDescriptor(src, keys[i]);
// Make it rewritable because two stamps can have same named getter/setter
desc.configurable = true;
defineProperty(dst, keys[i], desc);
}
}
return dst;
}

var assign = _Object.assign || _mergeOrAssign.bind(0, assignOne);
var assign = _mergeOrAssign.bind(0, assignOne);

function isFunction(obj) {
return typeof obj == 'function';
Expand Down Expand Up @@ -79,18 +89,20 @@
// Note that functions are also assigned! We do not deep merge functions.
if (!isPlainObject(src)) return src;

var keys = objectKeys(src), i = 0, key;
var keys = getOwnPropertyKeys(src), i = 0, key, desc;
for (; i < keys[_length];) {
key = keys[i++];

// Do not merge properties with the '_undefined' value.
if (src[key] !== _undefined) {
// deep merge each property. Recursion!
dst[key] = mergeOne(
// Recursive calls to mergeOne() must allow only plain objects or arrays in dst
isPlainObject(dst[key]) || isArray(src[key]) ? dst[key] : {},
src[key]
);
desc = getOwnPropertyDescriptor(src, key);
if (desc.hasOwnProperty('value')) { // is this a regular property?
// Do not merge properties with the 'undefined' value.
if (desc.value !== _undefined) {
// deep merge each property. Recursion!
dst[key] = mergeOne(isPlainObject(dst[key]) || isArray(src[key]) ? dst[key] : {}, src[key]);
}
} else { // nope, it looks like a getter/setter
// Make it rewritable because two stamps can have same named getter/setter
desc.configurable = true;
defineProperty(dst, key, desc);
}
}

Expand Down
4 changes: 2 additions & 2 deletions test/_standard-compliance-tests.js
@@ -1,5 +1,5 @@
import checkCompose from 'check-compose';
const checkCompose = require('check-compose');

import stampit from '../src/stampit';
const stampit = require('../src/stampit');

checkCompose(stampit);
4 changes: 2 additions & 2 deletions test/basics-init.js
@@ -1,5 +1,5 @@
import test from 'tape';
import stampit from '../src/stampit';
const test = require('tape');
const stampit = require('../src/stampit');

// Basics Enclose

Expand Down
4 changes: 2 additions & 2 deletions test/basics-methods.js
@@ -1,5 +1,5 @@
import test from 'tape';
import stampit from '../src/stampit';
const test = require('tape');
const stampit = require('../src/stampit');

// Basics Methods

Expand Down
4 changes: 2 additions & 2 deletions test/basics-props.js
@@ -1,5 +1,5 @@
import test from 'tape';
import stampit from '../src/stampit';
const test = require('tape');
const stampit = require('../src/stampit');

// Basics Props

Expand Down
4 changes: 2 additions & 2 deletions test/basics-static.js
@@ -1,5 +1,5 @@
import test from 'tape';
import stampit from '../src/stampit';
const test = require('tape');
const stampit = require('../src/stampit');

// Basics statics

Expand Down
4 changes: 2 additions & 2 deletions test/basics.js
@@ -1,5 +1,5 @@
import test from 'tape';
import stampit from '../src/stampit';
const test = require('tape');
const stampit = require('../src/stampit');

// Basics

Expand Down
4 changes: 2 additions & 2 deletions test/compose.js
@@ -1,5 +1,5 @@
import test from 'tape';
import stampit from '../src/stampit';
const test = require('tape');
const stampit = require('../src/stampit');

// Compose

Expand Down
6 changes: 3 additions & 3 deletions test/composers.js
@@ -1,6 +1,6 @@
import test from 'tape';
import _ from 'lodash';
import stampit from '../src/stampit';
const test = require('tape');
const _ = require('lodash');
const stampit = require('../src/stampit');

// composers

Expand Down
4 changes: 2 additions & 2 deletions test/convert-constructor.js
@@ -1,5 +1,5 @@
import test from 'tape';
import stampit from '../src/stampit';
const test = require('tape');
const stampit = require('../src/stampit');

// Oldskool

Expand Down
4 changes: 2 additions & 2 deletions test/corner-cases.js
@@ -1,5 +1,5 @@
import test from 'tape';
import stampit from '../src/stampit';
const test = require('tape');
const stampit = require('../src/stampit');

test('stamp.compose() deep merge bad deepProps', (t) => {
const stamp = stampit({props: {a: 1}});
Expand Down
4 changes: 2 additions & 2 deletions test/deep-props-safety.js
@@ -1,5 +1,5 @@
import test from 'tape';
import stampit from '../src/stampit';
const test = require('tape');
const stampit = require('../src/stampit');

// Props safety

Expand Down
4 changes: 2 additions & 2 deletions test/immutability.js
@@ -1,5 +1,5 @@
import test from 'tape';
import stampit from '../src/stampit';
const test = require('tape');
const stampit = require('../src/stampit');

// Immutability

Expand Down
18 changes: 3 additions & 15 deletions test/import.js
@@ -1,19 +1,7 @@
/* eslint-disable */
import test from 'tape';
const test = require('tape');

import stampit1 from '../';

test('import is the same as require', (t) => {
const stampit2 = require('../');

t.equal(stampit1, stampit2,
'Should export same object for both ES6 and CommonJS');

t.end();
});

test('infection works using the require("src/stampit")', t => {
const obj = require('../src/stampit')
test('infection works using the require("src/stampit")', (t) => {
const obj = require('../src/stampit') // eslint-disable-line global-require
.init(function () {
const secret = 'foo';
this.getSecret = () => { return secret; };
Expand Down
40 changes: 20 additions & 20 deletions test/index.js
@@ -1,20 +1,20 @@
import './_standard-compliance-tests';
import './basics-init';
import './basics-methods';
import './basics-props';
import './basics-static';
import './basics';
import './compose';
import './composers';
import './convert-constructor';
import './corner-cases';
import './deep-props-safety';
import './immutability';
import './import';
import './index';
import './infected-statics';
import './init';
import './name';
import './props';
import './stampit-api';
import './stampit-shortcuts';
require('./_standard-compliance-tests');
require('./basics-init');
require('./basics-methods');
require('./basics-props');
require('./basics-static');
require('./basics');
require('./compose');
require('./composers');
require('./convert-constructor');
require('./corner-cases');
require('./deep-props-safety');
require('./immutability');
require('./import');
require('./index');
require('./infected-statics');
require('./init');
require('./name');
require('./props');
require('./stampit-api');
require('./stampit-shortcuts');
7 changes: 4 additions & 3 deletions test/infected-statics.js
@@ -1,6 +1,7 @@
import test from 'tape';
import _ from 'lodash';
import stampit from '../src/stampit';
'use strict'; // eslint-disable-line
const test = require('tape');
const _ = require('lodash');
const stampit = require('../src/stampit');

test('stampit().methods static method', (t) => {
const methods = {method1() {}};
Expand Down
4 changes: 2 additions & 2 deletions test/init.js
@@ -1,5 +1,5 @@
import test from 'tape';
import stampit from '../src/stampit';
const test = require('tape');
const stampit = require('../src/stampit');

// Closure arguments

Expand Down
4 changes: 2 additions & 2 deletions test/name.js
@@ -1,5 +1,5 @@
import test from 'tape';
import stampit from '../src/stampit';
const test = require('tape');
const stampit = require('../src/stampit');

try {
const f = Object.defineProperties(
Expand Down
4 changes: 2 additions & 2 deletions test/props.js
@@ -1,5 +1,5 @@
import test from 'tape';
import stampit from '../src/stampit';
const test = require('tape');
const stampit = require('../src/stampit');

// props shallow mixing

Expand Down
4 changes: 2 additions & 2 deletions test/stampit-api.js
@@ -1,5 +1,5 @@
import test from 'tape';
import stampit from '../src/stampit';
const test = require('tape');
const stampit = require('../src/stampit');

function isFunction(obj) {
return typeof obj === 'function';
Expand Down
6 changes: 3 additions & 3 deletions test/stampit-shortcuts.js
@@ -1,6 +1,6 @@
import test from 'tape';
import _ from 'lodash';
import stampit from '../src/stampit';
const test = require('tape');
const _ = require('lodash');
const stampit = require('../src/stampit');

// stampit.methods, stampit.init, stampit.props, etc.

Expand Down

0 comments on commit c1a9a72

Please sign in to comment.