Skip to content

Commit

Permalink
Merge pull request #346 from stampit-org/getters-setters
Browse files Browse the repository at this point in the history
Getters and setters support
  • Loading branch information
koresar committed Sep 15, 2019
2 parents 11b1aaa + d3b47c0 commit 3a2617a
Show file tree
Hide file tree
Showing 29 changed files with 134 additions and 170 deletions.
18 changes: 0 additions & 18 deletions .eslintrc

This file was deleted.

4 changes: 2 additions & 2 deletions .gitignore
Expand Up @@ -46,8 +46,8 @@ build/Release
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
.npmrc


package-lock.json
.nyc_output

##### Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion
*.iml
Expand Down
2 changes: 2 additions & 0 deletions .npmignore
Expand Up @@ -11,3 +11,5 @@
/docs/
/src/
.npmrc
package-lock.json
.nyc_output
8 changes: 4 additions & 4 deletions .travis.yml
@@ -1,9 +1,9 @@
language: node_js
node_js:
- "4"
- "5"
- "6"
- "7"
- "8"
- "10"
- "11"
- "12"
script:
- npm run check
- npm run ci
65 changes: 33 additions & 32 deletions package.json
Expand Up @@ -28,47 +28,29 @@
},
"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",
"dependency-check": "^2.5.0",
"eslint": "^3.7.0",
"eslint-config-airbnb-base": "^11.0.0",
"eslint-config-es5": "^0.5.0",
"eslint-plugin-import": "^2.0.1",
"browserify": "^16.5.0",
"check-compose": "^5.0.0",
"eslint": "^6.4.0",
"gzip-size-cli": "^2.1.0",
"istanbul": "^1.1.0-alpha.1",
"lodash": "^4.16.1",
"lodash": "^4.17.15",
"mkdirp": "^0.5.1",
"nsp": "^2.1.0",
"nyc": "^14.1.1",
"require-all": "^2.2.0",
"rimraf": "^2.3.4",
"tape": "^4.2.2",
"uglify-js": "^2.8.29",
"watch": "^1.0.1"
"uglify-js": "^2.8.29"
},
"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": "nyc npm run test",
"pretest": "npm run build",
"test": "babel-node --presets=env test && npm run lint",
"test": "node test",
"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/*",
"lint": "eslint src && eslint test",
"prebuild": "npm run clean",
"browsertest": "mkdirp ./dist/ && browserify ./test/index.js > dist/test_bundle.js && cp ./test/index.html ./dist/ && open ./dist/index.html",
"lint": "eslint ./src/ ./test/",
"prebuild": "rm -rf ./dist/*",
"build": "npm run minify",
"deps": "npm run deps:missing && npm run deps:extra",
"deps:missing": "dependency-check package.json",
"deps:extra": "dependency-check package.json --extra --no-dev --ignore",
"audit": "nsp check",
"precheck": "npm test",
"check": "npm run audit && npm run deps",
"watch": "watch 'clear && npm -s test' test/ src/",
"ci": "npm run test",
"check": "npm run test && npm run lint",
"minify": "mkdirp ./dist/ && uglifyjs src/stampit.js -c collapse_vars,evaluate=false,screw_ie8,unsafe,loops=false,keep_fargs=false,pure_getters,unused,dead_code,keep_fnames=[\"'stampit','Stamp'\"] -m --reserved stampit,Stamp -o dist/stampit.min.js",
"preversion": "npm run check",
"postversion": "V=`node -e \"process.stdout.write(require('./package.json').version)\"` && sed s/VERSION/$V/g dist/stampit.min.js > dist/tmp && rm dist/stampit.min.js && mv dist/tmp dist/stampit.min.js",
Expand All @@ -83,5 +65,24 @@
"/dist/stampit.min.js"
]
}
]
],
"eslintConfig": {
"env": {
"browser": true,
"commonjs": true,
"es6": true,
"node": true
},
"extends": "eslint:recommended",
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"ecmaVersion": 2018
},
"rules": {
"no-prototype-builtins": 0
}
}
}
40 changes: 25 additions & 15 deletions src/stampit.js
Expand Up @@ -27,26 +27,35 @@
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
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 +88,19 @@
// 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
defineProperty(dst, key, desc);
}
}

Expand Down
13 changes: 1 addition & 12 deletions test/.eslintrc
@@ -1,16 +1,5 @@
// Add our overrides as necessary
{
"extends": "airbnb-base",
"rules": {
"object-shorthand": 0,
"arrow-body-style": 0,
"func-names": 0,
"newline-per-chained-call": 0,
"no-prototype-builtins": 0,
"import/no-named-as-default-member": 0,
"import/no-extraneous-dependencies": 0,
"comma-dangle": "off",
"object-curly-spacing": "off",
"no-param-reassign": "off"
"no-prototype-builtins": 0
}
}
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
10 changes: 0 additions & 10 deletions test/benchmark/.eslintrc

This file was deleted.

7 changes: 4 additions & 3 deletions test/benchmark/object-create.js
Expand Up @@ -2,7 +2,7 @@
const test = require('tape');

const Benchmark = require('benchmark');
const stampit = require('../..'); // Need to test the distributable
const stampit = require('../../'); // Need to test the distributable


test('benchmarking object creation', (t) => {
Expand Down Expand Up @@ -83,8 +83,9 @@ test('benchmarking object creation', (t) => {
results.push(String(event.target));
})
.on('complete', function () {
t.ok(this[0].hz / this[1].hz >= 0.02,
'creating a stamp should be less than 50 times slower, not more');
const MAX = 500;
t.ok(this[0].hz / this[1].hz >= 1 / MAX,
`creating a stamp should be less than ${MAX} times slower, but was ${this[1].hz / this[0].hz}`);
t.comment(results.join('. '));
t.end();
})
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

0 comments on commit 3a2617a

Please sign in to comment.