How to use the d/d.gs function in d

To help you get started, we’ve selected a few d examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github medikoo / dbjs / lib / history.js View on Github external
'use strict';

var last    = require('es5-ext/array/#/last')
  , forEach = require('es5-ext/object/for-each')
  , d       = require('d/d')
  , proto   = require('./_proto')

  , history = exports;

Object.defineProperties(proto, {
	_lastEvent_: d.gs(function () {
		var data = history.hasOwnProperty(this._id_) && history[this._id_];
		return (data && data[0]) || null;
	}),
	_lastModified_: d.gs(function () {
		var event = this._lastEvent_;
		return (event && event.stamp) || 0;
	}),
	_lastModifiedDate_: d.gs(function () {
		var lastModified = this._lastModified_;
		return lastModified ? new Date(lastModified / 1000) : null;
	})
});

Object.defineProperty(history, '_snapshot', d(function (/* options */) {
	var data = [], result, options = Object(arguments[0])
	  , property = options.property;
github medikoo / dbjs / lib / _relation / indexx.js View on Github external
// Remove from old, and add to new
		forEach(this, function (index, key) {
			forEach(index, function (obj) {
				if (old) old.delete(key, obj);
				if (nu) nu.add(key, obj);
			}, this);
		}, this);

		if (nu) this.parent = nu;
		else delete this.parent;
	})
});

Object.defineProperties(relation, {
	_index_: d.gs(function () {
		var proto;
		// Get index
		if (!this.obj || (this.obj._type_ !== 'object')) return null;
		proto = getPrototypeOf(this);
		while (proto.obj && (proto.obj._type_ !== 'prototype')) {
			proto = getPrototypeOf(proto);
		}
		if (!proto.obj) return null;
		return proto._selfIndex_;
	}),
	_selfIndex_: d.gs(function () {
		var index, parent;
		// Get index
		if (!this.obj || (this.obj._type_ !== 'prototype')) return null;
		if ((this.obj.ns._childType_ !== 'object') ||
				(this.obj._id_ === 'Object#')) {
github medikoo / dbjs / lib / history.js View on Github external
, forEach = require('es5-ext/object/for-each')
  , d       = require('d/d')
  , proto   = require('./_proto')

  , history = exports;

Object.defineProperties(proto, {
	_lastEvent_: d.gs(function () {
		var data = history.hasOwnProperty(this._id_) && history[this._id_];
		return (data && data[0]) || null;
	}),
	_lastModified_: d.gs(function () {
		var event = this._lastEvent_;
		return (event && event.stamp) || 0;
	}),
	_lastModifiedDate_: d.gs(function () {
		var lastModified = this._lastModified_;
		return lastModified ? new Date(lastModified / 1000) : null;
	})
});

Object.defineProperty(history, '_snapshot', d(function (/* options */) {
	var data = [], result, options = Object(arguments[0])
	  , property = options.property;
	forEach(history, function (events, id) {
		var event = events[0];
		if (!event || (event.value === undefined)) return;
		if ((property != null) && !event.obj.hasOwnProperty(property)) return;
		data[last.call(events).index] = events[0];
	});
	result = [];
	data.forEach(function (event) { result.push(event); });
github medikoo / dbjs / lib / _relation / simple.js View on Github external
descriptorCreate._update_.value = rel.__update_.bind(rel);
		defineProperties(rel, descriptorCreate);

		descriptor.value = rel;
		defineProperty(obj, '__' + this.name, descriptor);

		if (!this._noChangeListener_) this.on('change', rel._update_);
		return rel;
	}),
	__update_: d(function () {
		var nu = this._value, old = this.__value;
		if (old === nu) return;
		this.__value = nu;
		this.emit('change', nu, old);
	}),
	value: d.gs(function () {
		return this.__value;
	}, function (value) {
		var error = this.validate(value);
		if (error) throw error;
		this.$setValue(value);
	}),
	$setValue: d(function (value) {
		var ns;
		if (value != null) {
			ns = this.__ns.__value;
			value = ns.is(value) ? value : ns.prototype.$create(value);
		}
		this._signal_(value);
	}),
	$$setValue: d(function (value) {
		if (this.hasOwnProperty('_value')) {
github medikoo / dbjs / lib / _relation / set-read-only.js View on Github external
remove.call(this._value, item);
			this.emit('delete', item);
			changed = true;
		}
		if (this._value.length !== value.length) {
			data = diff.call(value, this._value);
			while ((item = data.shift()) != null) {
				this._value.push(item);
				this.emit('add', item);
				changed = true;
			}
		}
		defineProperty(this, 'count', d('c', value.length));
		return changed;
	}),
	values: d.gs(function () { return aFrom(this._value); })
});

ObjectList.defineOn(RelSet.prototype);
github medikoo / dbjs / lib / types / object.js View on Github external
byCreatedAt = function (a, b) { return a._lastModified_ - b._lastModified_; };

setExtensions = {
	listByCreatedAt: d(function () { return this.list(byCreatedAt); })
};

module.exports = ObjectType = defineProperties(Base.$$create('Object'), assign({
	_childType_: d('c', 'object'),
	_isSet_: d(true),
	obj: d.gs(function () { return this; }),
	add: d(readOnlySet.add),
	has: d(function (obj) {
		if (!obj) return false;
		return (this.hasOwnProperty(obj._id_) && (this[obj._id_] === obj));
	}),
	count: d.gs(function () { return this._count_; }),
	newNamed: d('c', function (name, value) {
		var error, args, obj;
		if (!nameRe.test(name)) throw new Error(name + " is invalid name");
		if (ObjectType.hasOwnProperty(name)) {
			throw new Error(name + " is already taken");
		}
		args = slice.call(arguments, 1);
		error = this.prototype.validateConstruction.apply(this.prototype, args);
		if (error) throw error;

		obj = this.prototype.$$create(name);
		obj._signal_(this.prototype);
		obj.$construct.apply(obj, args);
		return obj;
	}),
	_serialize_: d('c', function (value) { return '7' + value._id_; }),
github medikoo / dbjs / lib / _relation / set.js View on Github external
, d           = require('d/d')
  , serialize   = require('../utils/serialize')
  , unserialize = require('../utils/unserialize')
  , relation    = require('./')

  , isArray = Array.isArray, call = Function.prototype.call
  , defineProperties = Object.defineProperties
  , getPrototypeOf = Object.getPrototypeOf

  , relDelete = relation.delete, rel$delete = relation.$delete;

defineProperties(relation, {
	_assertSet_: d(function () {
		if (!this._isSet_) throw new TypeError("Property is not a set");
	}),
	count: d.gs(function () {
		var count;
		this._assertSet_();
		if (this.hasOwnProperty('_value')) return this._count_;
		count = 0;
		this.forEach(function () { ++count; });
		return count;
	}),
	_isSet_: d.gs(function () {
		return this.__multiple.__value &&
			((typeof this._value !== 'function') ||
				(this._value._type_ === 'namespace'));
	}),
	add: d(function (value) {
		var error;
		this._assertSet_();
		error = this.validateValue(value);
github medikoo / dbjs / lib / _relation / set-item.js View on Github external
if (defined && (this.obj._count_ === 1) && this.obj.required &&
				this.obj.hasOwnProperty('_value')) {
			throw new TypeError('Cannot remove the only value');
		}
		this.$delete();
		if (defined == null) this._signal_();
	}),
	$delete: d(function () {
		this._forEachRelation_(function (rel) { rel.$delete(); });
		if (this._value != null) this._signal_();
	})
});

defineProperties(relation, {
	__itemPrototype_: d('', item),
	_itemPrototype_: d.gs(function () {
		if (!this.hasOwnProperty('__itemPrototype_')) {
			this._fillRelationChain_('__itemPrototype_');
		}
		return this.__itemPrototype_;
	})
});
github medikoo / dbjs / lib / _proto / object-set.js View on Github external
module.exports = ObjectSet = function (obj) {
	defineProperties(this, {
		obj: d(obj),
		count: d('w', 0)
	});
};

ObjectSet.prototype = ee(Object.create(ReadOnlySet.prototype, {
	constructor: d(ObjectSet),
	has: d(function (obj) {
		var key = this._serialize(obj);
		if (key == null) return false;
		return this.hasOwnProperty(key);
	}),
	values: d.gs(function () { return keys(this).map(getValue, this); }),
	forEach: d(function (cb/*, thisArg*/) {
		var thisArg = arguments[1];
		callable(cb);
		keys(this).forEach(function (name) {
			call.call(cb, thisArg, this[name], null, this);
		}, this);
	})
}));

require('./object-map-set');
require('./object-filter-set');
require('./object-ordered-list');
require('./set-intersection');
require('./set-union');
require('./set-complement');
require('./list-by-property');
github medikoo / dbjs / lib / _proto / index.js View on Github external
return function (id) {
			var obj;
			Constructor.prototype = this;
			obj = new Constructor();
			descriptor.value = id;
			objects[id] = defineProperty(obj, '_id_', descriptor);
			if (!this.hasOwnProperty('_children_')) {
				descriptor.value = [];
				defineProperty(this, '_children_', descriptor);
				descriptor.value = null;
			}
			this._children_.push(obj);
			return obj;
		};
	}())),
	_prototypes_: d.gs(function () {
		var result = [], obj = this;
		if (obj === proto) return result;
		do {
			obj = getPrototypeOf(obj);
			result.push(obj);
		} while (obj !== proto);
		return result;
	})
});

require('./property');
require('./properties');
require('../history');
require('../signal');

d

Property descriptor factory

ISC
Latest version published 2 months ago

Package Health Score

72 / 100
Full package analysis