blueflag-record
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -37,4 +37,15 @@ "use strict"; | ||
foo: 'bar', | ||
baz: 'qux' | ||
baz: undefined | ||
}); | ||
var DateRecord = (0, _Record.default)({ | ||
start: { | ||
notSetValue: new Date('2000-01-01'), | ||
get: function get(value) { | ||
return new Date(value); | ||
}, | ||
set: function set(value) { | ||
return value.getFullYear(); | ||
} | ||
} | ||
}); | ||
describe('constructing', function () { | ||
@@ -77,2 +88,4 @@ it('it will throw for foriegn keys', function () { | ||
expect((0, _has.default)('foo')(foo)).toBeTruthy(); | ||
expect((0, _has.default)('baz')(foo)).toBeTruthy(); | ||
expect((0, _has.default)('other')(foo)).toBeFalsy(); | ||
}); | ||
@@ -82,3 +95,2 @@ it('supports the get function', function () { | ||
expect((0, _get.default)('foo')(foo)).toBe('bar'); | ||
expect((0, _get.default)('baz')(foo)).toBe('qux'); | ||
expect((0, _get.default)('baz', 'custom')(foo)).toBe('custom'); | ||
@@ -97,3 +109,3 @@ }); | ||
}); | ||
expect(data).toEqual([['foo', 'bar'], ['baz', 'qux']]); | ||
expect(data).toEqual([['foo', 'bar'], ['baz', undefined]]); | ||
}); | ||
@@ -111,5 +123,11 @@ it('supports the count function', function () { | ||
foo: 'radical', | ||
baz: 'qux' | ||
baz: undefined | ||
}); | ||
}); | ||
it('applies config.get to the value', function () { | ||
var date = new DateRecord({ | ||
start: '2001-01-01' | ||
}); | ||
expect(date.start).toEqual(new Date('2001-01-01')); | ||
}); | ||
}); | ||
@@ -154,6 +172,12 @@ describe('setters', function () { | ||
}); | ||
it('applies config.set to the new value', function () { | ||
var date = new DateRecord({ | ||
start: '2001-01-01' | ||
}).set('start', new Date('2222-01-01')); | ||
expect(date._data.start).toBe(2222); | ||
}); | ||
}); | ||
it('supports property accessors', function () { | ||
var foo = new FooRecord(); | ||
expect(foo.baz).toBe('qux'); | ||
expect(foo.foo).toBe('bar'); | ||
expect(function () { | ||
@@ -164,5 +188,6 @@ return foo.baz = 'wrong!'; | ||
describe('merging', function () { | ||
it('it will merge data before default values', function () { | ||
it('will merge only data not default values', function () { | ||
var previous = new FooRecord({ | ||
foo: 1 | ||
foo: 1, | ||
baz: 'qux' | ||
}); | ||
@@ -176,2 +201,20 @@ var next = new FooRecord({ | ||
}); | ||
it('will parse new values through setters', function () { | ||
var previous = new DateRecord({}); | ||
var next = new DateRecord({ | ||
start: new Date('2001-01-01') | ||
}); | ||
var merged = previous.merge({ | ||
start: new Date('2001-01-01') | ||
}); | ||
expect(merged._data.start).toBe(2001); | ||
}); | ||
it('can merge records or objects', function () { | ||
var newData = { | ||
start: new Date('1984-01-01') | ||
}; | ||
var previous = new DateRecord({}); | ||
expect(previous.merge(newData)._data.start).toBe(1984); | ||
expect(previous.merge(new DateRecord(newData))._data.start).toBe(1984); | ||
}); | ||
}); |
@@ -18,3 +18,3 @@ "use strict"; | ||
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); | ||
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof")); | ||
@@ -33,2 +33,4 @@ var _delete = _interopRequireDefault(require("unmutable/lib/delete")); | ||
var _map = _interopRequireDefault(require("unmutable/lib/map")); | ||
var _set = _interopRequireDefault(require("unmutable/lib/set")); | ||
@@ -42,3 +44,31 @@ | ||
function RecordFactory(notSetValues) { | ||
var indentity = function indentity(x) { | ||
return x; | ||
}; | ||
var nonEnumerable = function nonEnumerable(vv) { | ||
return { | ||
enumerable: false, | ||
value: vv | ||
}; | ||
}; | ||
function RecordFactory(config) { | ||
var keyConfig = (0, _map.default)(function (vv) { | ||
return (0, _typeof2.default)(vv) === 'object' ? vv : { | ||
notSetValues: vv | ||
}; | ||
})(config); | ||
var notSetValues = (0, _map.default)(function (vv) { | ||
return vv && vv.notSetValue || vv; | ||
})(config); | ||
var setter = function setter(key, value) { | ||
return ((keyConfig[key] || {}).set || indentity)(value); | ||
}; | ||
var getter = function getter(key, value) { | ||
return ((keyConfig[key] || {}).get || indentity)(value); | ||
}; | ||
return ( | ||
@@ -52,38 +82,62 @@ /*#__PURE__*/ | ||
(0, _classCallCheck2.default)(this, Record); | ||
(0, _defineProperty2.default)(this, "has", function (key) { | ||
return (0, _has.default)(key)(_this._data); | ||
Object.defineProperties(this, { | ||
// Private values | ||
__UNMUTABLE_COMPATIBLE__: nonEnumerable(true), | ||
_data: nonEnumerable(data), | ||
_notSetValues: nonEnumerable(notSetValues), | ||
// Methods | ||
unit: nonEnumerable(function (data) { | ||
return new _this.constructor(data); | ||
}), | ||
toObject: nonEnumerable(function () { | ||
return (0, _objectSpread2.default)({}, _this._notSetValues, _this._data); | ||
}), | ||
toJSON: nonEnumerable(function () { | ||
return _this.toObject(); | ||
}), | ||
has: nonEnumerable(function (key) { | ||
return (0, _has.default)(key)(_this._notSetValues); | ||
}), | ||
get: nonEnumerable(function (key, notFoundValue) { | ||
var value = _this._data[key]; | ||
if (value !== undefined) { | ||
return (keyConfig[key].get || indentity)(value); | ||
} | ||
return notFoundValue || (0, _get.default)(key)(_this._notSetValues); | ||
}), | ||
getIn: nonEnumerable(function (path, notFoundValue) { | ||
return (0, _getIn.default)(path, notFoundValue === undefined ? (0, _getIn.default)(path)(_this._notSetValues) : notFoundValue)(_this._data); | ||
}), | ||
set: nonEnumerable(function (key, childValue) { | ||
var value = setter(key, childValue); | ||
return _this.unit((0, _set.default)(key, value)(_this._data)); | ||
}), | ||
setIn: nonEnumerable(function (path, childValue) { | ||
return _this.unit((0, _setIn.default)(path, childValue)(_this._data)); | ||
}), | ||
delete: nonEnumerable(function (key) { | ||
return _this.unit((0, _delete.default)(key)(_this._data)); | ||
}), | ||
entries: nonEnumerable(function () { | ||
return (0, _entries.default)()(_this.toObject()); | ||
}), | ||
merge: nonEnumerable(function (next) { | ||
// prepare a function to run the new values through their setter | ||
var updateValues = (0, _map.default)(function (value, key) { | ||
return setter(key, value); | ||
}); | ||
return _this.unit((0, _objectSpread2.default)({}, _this._data, updateValues(next._data || next))); | ||
}), | ||
clear: nonEnumerable(function () { | ||
return _this.unit({}); | ||
}), | ||
clone: nonEnumerable(function () { | ||
return _this.unit(_this._data); | ||
}), | ||
count: nonEnumerable(function () { | ||
return (0, _toConsumableArray2.default)(_this.entries()).length; | ||
}) | ||
}); | ||
(0, _defineProperty2.default)(this, "get", function (key, notSetValue) { | ||
return (0, _get.default)(key, notSetValue || (0, _get.default)(key)(_this._notSetValues))(_this._data); | ||
}); | ||
(0, _defineProperty2.default)(this, "getIn", function (path, notSetValue) { | ||
return (0, _getIn.default)(path, notSetValue === undefined ? (0, _getIn.default)(path)(_this._notSetValues) : notSetValue)(_this._data); | ||
}); | ||
(0, _defineProperty2.default)(this, "set", function (key, childValue) { | ||
return _this.unit((0, _set.default)(key, childValue)(_this._data)); | ||
}); | ||
(0, _defineProperty2.default)(this, "setIn", function (path, childValue) { | ||
return _this.unit((0, _setIn.default)(path, childValue)(_this._data)); | ||
}); | ||
(0, _defineProperty2.default)(this, "delete", function (key) { | ||
return _this.unit((0, _delete.default)(key)(_this._data)); | ||
}); | ||
(0, _defineProperty2.default)(this, "entries", function () { | ||
return (0, _entries.default)()(_this.toObject()); | ||
}); | ||
(0, _defineProperty2.default)(this, "merge", function (next) { | ||
return _this.unit((0, _objectSpread2.default)({}, _this._data, (0, _toObject.default)()(next))); | ||
}); | ||
(0, _defineProperty2.default)(this, "clear", function () { | ||
return _this.unit({}); | ||
}); | ||
(0, _defineProperty2.default)(this, "clone", function () { | ||
return _this.unit(_this._data); | ||
}); | ||
(0, _defineProperty2.default)(this, "count", function () { | ||
return (0, _toConsumableArray2.default)(_this.entries()).length; | ||
}); | ||
this.__UNMUTABLE_COMPATIBLE__ = true; | ||
this._data = data; | ||
this._notSetValues = notSetValues; | ||
Object.keys(data).forEach(function (key) { | ||
@@ -94,3 +148,3 @@ if (!notSetValues.hasOwnProperty(key)) { | ||
}); | ||
Object.keys(notSetValues).forEach(function (key) { | ||
Object.keys(keyConfig).forEach(function (key) { | ||
Object.defineProperty(_this, key, { | ||
@@ -108,13 +162,3 @@ enumerable: true, | ||
(0, _createClass2.default)(Record, [{ | ||
key: "unit", | ||
value: function unit(data) { | ||
return new this.constructor(data); | ||
} | ||
}, { | ||
key: "toObject", | ||
value: function toObject() { | ||
return (0, _objectSpread2.default)({}, this._notSetValues, this._data); | ||
} | ||
}], [{ | ||
(0, _createClass2.default)(Record, null, [{ | ||
key: "fromUnknown", | ||
@@ -121,0 +165,0 @@ value: function fromUnknown(unknown) { |
{ | ||
"main": "lib/index.js", | ||
"name": "blueflag-record", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "An Unmutable compatible record", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
14744
351