package/package.json000666 000765 000024 0000001554 12630275343013033 0ustar00000000 000000 { "name": "d", "version": "1.0.0", "description": "Property descriptor factory", "author": "Mariusz Nowak (http://www.medikoo.com/)", "keywords": [ "descriptor", "es", "ecmascript", "ecma", "property", "descriptors", "meta", "properties" ], "repository": { "type": "git", "url": "git://github.com/medikoo/d.git" }, "dependencies": { "es5-ext": "^0.10.9" }, "devDependencies": { "tad": "^0.2.4", "xlint": "^0.2.2", "xlint-jslint-medikoo": "^0.1.4" }, "scripts": { "lint": "node node_modules/xlint/bin/xlint --linter=node_modules/xlint-jslint-medikoo/index.js --no-cache --no-stream", "lint-console": "node node_modules/xlint/bin/xlint --linter=node_modules/xlint-jslint-medikoo/index.js --watch", "test": "node node_modules/tad/bin/tad" }, "license": "MIT" } package/.npmignore000666 000765 000024 0000000063 12542777637012555 0ustar00000000 000000 .DS_Store /node_modules /npm-debug.log /.lintcache package/README.md000666 000765 000024 0000005244 12542777637012043 0ustar00000000 000000 # D ## Property descriptor factory _Originally derived from [es5-ext](https://github.com/medikoo/es5-ext) package._ Defining properties with descriptors is very verbose: ```javascript var Account = function () {}; Object.defineProperties(Account.prototype, { deposit: { value: function () { /* ... */ }, configurable: true, enumerable: false, writable: true }, withdraw: { value: function () { /* ... */ }, configurable: true, enumerable: false, writable: true }, balance: { get: function () { /* ... */ }, configurable: true, enumerable: false } }); ``` D cuts that to: ```javascript var d = require('d'); var Account = function () {}; Object.defineProperties(Account.prototype, { deposit: d(function () { /* ... */ }), withdraw: d(function () { /* ... */ }), balance: d.gs(function () { /* ... */ }) }); ``` By default, created descriptor follow characteristics of native ES5 properties, and defines values as: ```javascript { configurable: true, enumerable: false, writable: true } ``` You can overwrite it by preceding _value_ argument with instruction: ```javascript d('c', value); // { configurable: true, enumerable: false, writable: false } d('ce', value); // { configurable: true, enumerable: true, writable: false } d('e', value); // { configurable: false, enumerable: true, writable: false } // Same way for get/set: d.gs('e', value); // { configurable: false, enumerable: true } ``` ### Installation $ npm install d To port it to Browser or any other (non CJS) environment, use your favorite CJS bundler. No favorite yet? Try: [Browserify](http://browserify.org/), [Webmake](https://github.com/medikoo/modules-webmake) or [Webpack](http://webpack.github.io/) ### Other utilities #### autoBind(obj, props) _(d/auto-bind)_ Define methods which will be automatically bound to its instances ```javascript var d = require('d'); var autoBind = require('d/auto-bind'); var Foo = function () { this._count = 0; }; Object.defineProperties(Foo.prototype, autoBind({ increment: d(function () { ++this._count; }); })); var foo = new Foo(); // Increment foo counter on each domEl click domEl.addEventListener('click', foo.increment, false); ``` #### lazy(obj, props) _(d/lazy)_ Define lazy properties, which will be resolved on first access ```javascript var d = require('d'); var lazy = require('d/lazy'); var Foo = function () {}; Object.defineProperties(Foo.prototype, lazy({ items: d(function () { return []; }) })); var foo = new Foo(); foo.items.push(1, 2); // foo.items array created and defined directly on foo ``` ## Tests [![Build Status](https://travis-ci.org/medikoo/d.png)](https://travis-ci.org/medikoo/d) $ npm test package/LICENSE000666 000765 000024 0000002063 12542777637011565 0ustar00000000 000000 Copyright (C) 2013 Mariusz Nowak (www.medikoo.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. package/lazy.js000666 000765 000024 0000007445 12565350220012062 0ustar00000000 000000 'use strict'; var map = require('es5-ext/object/map') , isCallable = require('es5-ext/object/is-callable') , validValue = require('es5-ext/object/valid-value') , contains = require('es5-ext/string/#/contains') , call = Function.prototype.call , defineProperty = Object.defineProperty , getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor , getPrototypeOf = Object.getPrototypeOf , hasOwnProperty = Object.prototype.hasOwnProperty , cacheDesc = { configurable: false, enumerable: false, writable: false, value: null } , define; define = function (name, options) { var value, dgs, cacheName, desc, writable = false, resolvable , flat; options = Object(validValue(options)); cacheName = options.cacheName; flat = options.flat; if (cacheName == null) cacheName = name; delete options.cacheName; value = options.value; resolvable = isCallable(value); delete options.value; dgs = { configurable: Boolean(options.configurable), enumerable: Boolean(options.enumerable) }; if (name !== cacheName) { dgs.get = function () { if (hasOwnProperty.call(this, cacheName)) return this[cacheName]; cacheDesc.value = resolvable ? call.call(value, this, options) : value; cacheDesc.writable = writable; defineProperty(this, cacheName, cacheDesc); cacheDesc.value = null; if (desc) defineProperty(this, name, desc); return this[cacheName]; }; } else if (!flat) { dgs.get = function self() { var ownDesc; if (hasOwnProperty.call(this, name)) { ownDesc = getOwnPropertyDescriptor(this, name); // It happens in Safari, that getter is still called after property // was defined with a value, following workarounds that // While in IE11 it may happen that here ownDesc is undefined (go figure) if (ownDesc) { if (ownDesc.hasOwnProperty('value')) return ownDesc.value; if ((typeof ownDesc.get === 'function') && (ownDesc.get !== self)) { return ownDesc.get.call(this); } return value; } } desc.value = resolvable ? call.call(value, this, options) : value; defineProperty(this, name, desc); desc.value = null; return this[name]; }; } else { dgs.get = function self() { var base = this, ownDesc; if (hasOwnProperty.call(this, name)) { // It happens in Safari, that getter is still called after property // was defined with a value, following workarounds that ownDesc = getOwnPropertyDescriptor(this, name); if (ownDesc.hasOwnProperty('value')) return ownDesc.value; if ((typeof ownDesc.get === 'function') && (ownDesc.get !== self)) { return ownDesc.get.call(this); } } while (!hasOwnProperty.call(base, name)) base = getPrototypeOf(base); desc.value = resolvable ? call.call(value, base, options) : value; defineProperty(base, name, desc); desc.value = null; return base[name]; }; } dgs.set = function (value) { if (hasOwnProperty.call(this, name)) { throw new TypeError("Cannot assign to lazy defined '" + name + "' property of " + this); } dgs.get.call(this); this[cacheName] = value; }; if (options.desc) { desc = { configurable: contains.call(options.desc, 'c'), enumerable: contains.call(options.desc, 'e') }; if (cacheName === name) { desc.writable = contains.call(options.desc, 'w'); desc.value = null; } else { writable = contains.call(options.desc, 'w'); desc.get = dgs.get; desc.set = dgs.set; } delete options.desc; } else if (cacheName === name) { desc = { configurable: Boolean(options.configurable), enumerable: Boolean(options.enumerable), writable: Boolean(options.writable), value: null }; } delete options.configurable; delete options.enumerable; delete options.writable; return dgs; }; module.exports = function (props) { return map(props, function (desc, name) { return define(name, desc); }); }; package/index.js000666 000765 000024 0000002713 12542777637012227 0ustar00000000 000000 'use strict'; var assign = require('es5-ext/object/assign') , normalizeOpts = require('es5-ext/object/normalize-options') , isCallable = require('es5-ext/object/is-callable') , contains = require('es5-ext/string/#/contains') , d; d = module.exports = function (dscr, value/*, options*/) { var c, e, w, options, desc; if ((arguments.length < 2) || (typeof dscr !== 'string')) { options = value; value = dscr; dscr = null; } else { options = arguments[2]; } if (dscr == null) { c = w = true; e = false; } else { c = contains.call(dscr, 'c'); e = contains.call(dscr, 'e'); w = contains.call(dscr, 'w'); } desc = { value: value, configurable: c, enumerable: e, writable: w }; return !options ? desc : assign(normalizeOpts(options), desc); }; d.gs = function (dscr, get, set/*, options*/) { var c, e, options, desc; if (typeof dscr !== 'string') { options = set; set = get; get = dscr; dscr = null; } else { options = arguments[3]; } if (get == null) { get = undefined; } else if (!isCallable(get)) { options = get; get = set = undefined; } else if (set == null) { set = undefined; } else if (!isCallable(set)) { options = set; set = undefined; } if (dscr == null) { c = true; e = false; } else { c = contains.call(dscr, 'c'); e = contains.call(dscr, 'e'); } desc = { get: get, set: set, configurable: c, enumerable: e }; return !options ? desc : assign(normalizeOpts(options), desc); }; package/auto-bind.js000666 000765 000024 0000002310 12630273573012757 0ustar00000000 000000 'use strict'; var copy = require('es5-ext/object/copy') , normalizeOptions = require('es5-ext/object/normalize-options') , ensureCallable = require('es5-ext/object/valid-callable') , map = require('es5-ext/object/map') , callable = require('es5-ext/object/valid-callable') , validValue = require('es5-ext/object/valid-value') , bind = Function.prototype.bind, defineProperty = Object.defineProperty , hasOwnProperty = Object.prototype.hasOwnProperty , define; define = function (name, desc, options) { var value = validValue(desc) && callable(desc.value), dgs; dgs = copy(desc); delete dgs.writable; delete dgs.value; dgs.get = function () { if (!options.overwriteDefinition && hasOwnProperty.call(this, name)) return value; desc.value = bind.call(value, options.resolveContext ? options.resolveContext(this) : this); defineProperty(this, name, desc); return this[name]; }; return dgs; }; module.exports = function (props/*, options*/) { var options = normalizeOptions(arguments[1]); if (options.resolveContext != null) ensureCallable(options.resolveContext); return map(props, function (desc, name) { return define(name, desc, options); }); }; package/.lint000666 000765 000024 0000000074 12542777637011527 0ustar00000000 000000 @root module tabs indent 2 maxlen 100 ass nomen plusplus package/CHANGES000666 000765 000024 0000000670 12630274771011542 0ustar00000000 000000 v1.0.0 -- 2015.12.04 - autoBind changes: - replace `bindTo` argument with options and `resolveContext` option - Add support `overwriteDefinition` - Introduce IE11 bug workaround in `lazy` handler v0.1.1 -- 2014.04.24 - Add `autoBind` and `lazy` utilities - Allow to pass other options to be merged onto created descriptor. Useful when used with other custom utilties v0.1.0 -- 2013.06.20 Initial (derived from es5-ext project) package/.travis.yml000666 000765 000024 0000000440 12630275153012646 0ustar00000000 000000 sudo: false # http://docs.travis-ci.com/user/workers/container-based-infrastructure/ language: node_js node_js: - 0.12 - 4 - 5 before_install: - mkdir node_modules; ln -s ../ node_modules/d notifications: email: - medikoo+d@medikoo.com script: "npm test && npm run lint" package/test/auto-bind.js000666 000765 000024 0000000401 12542777637013751 0ustar00000000 000000 'use strict'; var d = require('../'); module.exports = function (t, a) { var o = Object.defineProperties({}, t({ bar: d(function () { return this === o; }), bar2: d(function () { return this; }) })); a.deep([(o.bar)(), (o.bar2)()], [true, o]); }; package/test/index.js000666 000765 000024 0000013700 12542777637013204 0ustar00000000 000000 'use strict'; var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor; module.exports = function (t, a) { var o, c, cg, cs, ce, ceg, ces, cew, cw, e, eg, es, ew, v, vg, vs, w, df, dfg , dfs; o = Object.create(Object.prototype, { c: t('c', c = {}), cgs: t.gs('c', cg = function () {}, cs = function () {}), ce: t('ce', ce = {}), cegs: t.gs('ce', ceg = function () {}, ces = function () {}), cew: t('cew', cew = {}), cw: t('cw', cw = {}), e: t('e', e = {}), egs: t.gs('e', eg = function () {}, es = function () {}), ew: t('ew', ew = {}), v: t('', v = {}), vgs: t.gs('', vg = function () {}, vs = function () {}), w: t('w', w = {}), df: t(df = {}), dfgs: t.gs(dfg = function () {}, dfs = function () {}) }); return { c: function (a) { var d = getOwnPropertyDescriptor(o, 'c'); a(d.value, c, "Value"); a(d.get, undefined, "Get"); a(d.set, undefined, "Set"); a(d.configurable, true, "Configurable"); a(d.enumerable, false, "Enumerable"); a(d.writable, false, "Writable"); d = getOwnPropertyDescriptor(o, 'cgs'); a(d.value, undefined, "GS Value"); a(d.get, cg, "GS Get"); a(d.set, cs, "GS Set"); a(d.configurable, true, "GS Configurable"); a(d.enumerable, false, "GS Enumerable"); a(d.writable, undefined, "GS Writable"); }, ce: function (a) { var d = getOwnPropertyDescriptor(o, 'ce'); a(d.value, ce, "Value"); a(d.get, undefined, "Get"); a(d.set, undefined, "Set"); a(d.configurable, true, "Configurable"); a(d.enumerable, true, "Enumerable"); a(d.writable, false, "Writable"); d = getOwnPropertyDescriptor(o, 'cegs'); a(d.value, undefined, "GS Value"); a(d.get, ceg, "GS Get"); a(d.set, ces, "GS Set"); a(d.configurable, true, "GS Configurable"); a(d.enumerable, true, "GS Enumerable"); a(d.writable, undefined, "GS Writable"); }, cew: function (a) { var d = getOwnPropertyDescriptor(o, 'cew'); a(d.value, cew, "Value"); a(d.get, undefined, "Get"); a(d.set, undefined, "Set"); a(d.configurable, true, "Configurable"); a(d.enumerable, true, "Enumerable"); a(d.writable, true, "Writable"); }, cw: function (a) { var d = getOwnPropertyDescriptor(o, 'cw'); a(d.value, cw, "Value"); a(d.get, undefined, "Get"); a(d.set, undefined, "Set"); a(d.configurable, true, "Configurable"); a(d.enumerable, false, "Enumerable"); a(d.writable, true, "Writable"); }, e: function (a) { var d = getOwnPropertyDescriptor(o, 'e'); a(d.value, e, "Value"); a(d.get, undefined, "Get"); a(d.set, undefined, "Set"); a(d.configurable, false, "Configurable"); a(d.enumerable, true, "Enumerable"); a(d.writable, false, "Writable"); d = getOwnPropertyDescriptor(o, 'egs'); a(d.value, undefined, "GS Value"); a(d.get, eg, "GS Get"); a(d.set, es, "GS Set"); a(d.configurable, false, "GS Configurable"); a(d.enumerable, true, "GS Enumerable"); a(d.writable, undefined, "GS Writable"); }, ew: function (a) { var d = getOwnPropertyDescriptor(o, 'ew'); a(d.value, ew, "Value"); a(d.get, undefined, "Get"); a(d.set, undefined, "Set"); a(d.configurable, false, "Configurable"); a(d.enumerable, true, "Enumerable"); a(d.writable, true, "Writable"); }, v: function (a) { var d = getOwnPropertyDescriptor(o, 'v'); a(d.value, v, "Value"); a(d.get, undefined, "Get"); a(d.set, undefined, "Set"); a(d.configurable, false, "Configurable"); a(d.enumerable, false, "Enumerable"); a(d.writable, false, "Writable"); d = getOwnPropertyDescriptor(o, 'vgs'); a(d.value, undefined, "GS Value"); a(d.get, vg, "GS Get"); a(d.set, vs, "GS Set"); a(d.configurable, false, "GS Configurable"); a(d.enumerable, false, "GS Enumerable"); a(d.writable, undefined, "GS Writable"); }, w: function (a) { var d = getOwnPropertyDescriptor(o, 'w'); a(d.value, w, "Value"); a(d.get, undefined, "Get"); a(d.set, undefined, "Set"); a(d.configurable, false, "Configurable"); a(d.enumerable, false, "Enumerable"); a(d.writable, true, "Writable"); }, d: function (a) { var d = getOwnPropertyDescriptor(o, 'df'); a(d.value, df, "Value"); a(d.get, undefined, "Get"); a(d.set, undefined, "Set"); a(d.configurable, true, "Configurable"); a(d.enumerable, false, "Enumerable"); a(d.writable, true, "Writable"); d = getOwnPropertyDescriptor(o, 'dfgs'); a(d.value, undefined, "GS Value"); a(d.get, dfg, "GS Get"); a(d.set, dfs, "GS Set"); a(d.configurable, true, "GS Configurable"); a(d.enumerable, false, "GS Enumerable"); a(d.writable, undefined, "GS Writable"); }, Options: { v: function (a) { var x = {}, d = t(x, { foo: true }); a.deep(d, { configurable: true, enumerable: false, writable: true, value: x, foo: true }, "No descriptor"); d = t('c', 'foo', { marko: 'elo' }); a.deep(d, { configurable: true, enumerable: false, writable: false, value: 'foo', marko: 'elo' }, "Descriptor"); }, gs: function (a) { var gFn = function () {}, sFn = function () {}, d; d = t.gs(gFn, sFn, { foo: true }); a.deep(d, { configurable: true, enumerable: false, get: gFn, set: sFn, foo: true }, "No descriptor"); d = t.gs(null, sFn, { foo: true }); a.deep(d, { configurable: true, enumerable: false, get: undefined, set: sFn, foo: true }, "No descriptor: Just set"); d = t.gs(gFn, { foo: true }); a.deep(d, { configurable: true, enumerable: false, get: gFn, set: undefined, foo: true }, "No descriptor: Just get"); d = t.gs('e', gFn, sFn, { bar: true }); a.deep(d, { configurable: false, enumerable: true, get: gFn, set: sFn, bar: true }, "Descriptor"); d = t.gs('e', null, sFn, { bar: true }); a.deep(d, { configurable: false, enumerable: true, get: undefined, set: sFn, bar: true }, "Descriptor: Just set"); d = t.gs('e', gFn, { bar: true }); a.deep(d, { configurable: false, enumerable: true, get: gFn, set: undefined, bar: true }, "Descriptor: Just get"); } } }; }; package/test/lazy.js000666 000765 000024 0000005305 12542777637013056 0ustar00000000 000000 'use strict'; var d = require('../') , getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor; module.exports = function (t, a) { var Foo = function () {}, i = 1, o, o2, desc; Object.defineProperties(Foo.prototype, t({ bar: d(function () { return ++i; }), bar2: d(function () { return this.bar + 23; }), bar3: d(function () { return this.bar2 + 34; }, { desc: 'ew' }), bar4: d(function () { return this.bar3 + 12; }, { cacheName: '_bar4_' }), bar5: d(function () { return this.bar4 + 3; }, { cacheName: '_bar5_', desc: 'e' }) })); desc = getOwnPropertyDescriptor(Foo.prototype, 'bar'); a(desc.configurable, true, "Configurable: default"); a(desc.enumerable, false, "Enumerable: default"); o = new Foo(); a.deep([o.bar, o.bar2, o.bar3, o.bar4, o.bar5], [2, 25, 59, 71, 74], "Values"); a.deep(getOwnPropertyDescriptor(o, 'bar3'), { configurable: false, enumerable: true, writable: true, value: 59 }, "Desc"); a(o.hasOwnProperty('bar4'), false, "Cache not exposed"); desc = getOwnPropertyDescriptor(o, 'bar5'); a.deep(desc, { configurable: false, enumerable: true, get: desc.get, set: desc.set }, "Cache & Desc: desc"); o2 = Object.create(o); o2.bar = 30; o2.bar3 = 100; a.deep([o2.bar, o2.bar2, o2.bar3, o2.bar4, o2.bar5], [30, 25, 100, 112, 115], "Extension Values"); Foo = function () {}; Object.defineProperties(Foo.prototype, t({ test: d('w', function () { return 'raz'; }), test2: d('', function () { return 'raz'; }, { desc: 'w' }), test3: d('', function () { return 'raz'; }, { cacheName: '__test3__', desc: 'w' }), test4: d('w', 'bar') })); o = new Foo(); o.test = 'marko'; a.deep(getOwnPropertyDescriptor(o, 'test'), { configurable: false, enumerable: false, writable: true, value: 'marko' }, "Set before get"); o.test2 = 'marko2'; a.deep(getOwnPropertyDescriptor(o, 'test2'), { configurable: false, enumerable: false, writable: true, value: 'marko2' }, "Set before get: Custom desc"); o.test3 = 'marko3'; a.deep(getOwnPropertyDescriptor(o, '__test3__'), { configurable: false, enumerable: false, writable: true, value: 'marko3' }, "Set before get: Custom cache name"); a(o.test4, 'bar', "Resolve by value"); a.h1("Flat"); Object.defineProperties(Foo.prototype, t({ flat: d(function () { return 'foo'; }, { flat: true }), flat2: d(function () { return 'bar'; }, { flat: true }) })); a.h2("Instance"); a(o.flat, 'foo', "Value"); a(o.hasOwnProperty('flat'), false, "Instance"); a(Foo.prototype.flat, 'foo', "Prototype"); a.h2("Direct"); a(Foo.prototype.flat2, 'bar'); a.h2("Reset direct"); Object.defineProperties(Foo.prototype, t({ testResetDirect: d(false) })); a.throws(function () { Foo.prototype.testResetDirect = false; }, TypeError); };