@sanity/mutator
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -35,10 +35,10 @@ 'use strict'; | ||
target.toIndicies(accessor).forEach(function (i) { | ||
var value = accessor.getIndexRaw(i); | ||
var newValue = dmp.patch_apply(_this.dmpPatch, value)[0]; | ||
accessor.setIndexRaw(i, newValue); | ||
accessor.getIndex(i).mutate(function (value) { | ||
return dmp.patch_apply(_this.dmpPatch, value)[0]; | ||
}); | ||
}); | ||
} else if (target.isAttributeReference()) { | ||
var value = accessor.getRaw(target.name()); | ||
var newValue = dmp.patch_apply(_this.dmpPatch, value)[0]; | ||
accessor.setRaw(target.name(), newValue); | ||
accessor.get(target.name()).mutate(function (value) { | ||
return dmp.patch_apply(_this.dmpPatch, value)[0]; | ||
}); | ||
} else { | ||
@@ -45,0 +45,0 @@ throw new Error('Unable to apply diffMatchPatch to target ' + target.toString()); |
@@ -125,11 +125,3 @@ "use strict"; | ||
} | ||
// The key exists and the value is not null | ||
}, { | ||
key: "isSet", | ||
value: function isSet(key) { | ||
var value = this.getRaw(key); | ||
return value !== null && typeof value != 'undefined'; | ||
} | ||
}, { | ||
key: "getIndexRaw", | ||
@@ -160,4 +152,49 @@ value: function getIndexRaw(i) { | ||
}, { | ||
key: "delete", | ||
value: function _delete(key) { | ||
this._mutate(function (value) { | ||
delete value[key]; | ||
return value; | ||
}); | ||
} | ||
}, { | ||
key: "deleteIndicies", | ||
value: function deleteIndicies(indicies) { | ||
this._mutate(function (value) { | ||
var length = value.length; | ||
var newValue = []; | ||
// Copy every value _not_ in the indicies array over to the newValue | ||
for (var i = 0; i < length; i++) { | ||
if (indicies.indexOf(i) == -1) { | ||
newValue.push(value[i]); | ||
} | ||
} | ||
return newValue; | ||
}); | ||
} | ||
}, { | ||
key: "insert", | ||
value: function insert(pos, items) { | ||
var _this3 = this; | ||
this._mutate(function (value) { | ||
if (_this3.getLength() == 0 && pos == 0) { | ||
return items; | ||
} | ||
return value.slice(0, pos).concat(items).concat(value.slice(pos)); | ||
}); | ||
} | ||
}, { | ||
key: "mutate", | ||
value: function mutate(fn) { | ||
if (!this.isPrimitiveValue()) { | ||
throw new Error("Won't mutate container types"); | ||
} | ||
this._mutate(fn); | ||
} | ||
// Not part of the Accessor protocol | ||
}, { | ||
key: "_mutate", | ||
value: function _mutate(fn) { | ||
var val = this.getter(); | ||
@@ -164,0 +201,0 @@ // Make sure we send a copy to the mutator |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -20,3 +20,3 @@ Object.defineProperty(exports, "__esModule", { | ||
_createClass(Set, [{ | ||
key: 'apply', | ||
key: "apply", | ||
value: function apply(targets, accessor) { | ||
@@ -28,14 +28,12 @@ var _this = this; | ||
target.toIndicies(accessor).forEach(function (i) { | ||
var val = accessor.getIndexRaw(i); | ||
if (typeof val == 'number') { | ||
accessor.setIndexRaw(i, val + _this.value); | ||
} | ||
accessor.getIndex(i).mutate(function (value) { | ||
return value + _this.value; | ||
}); | ||
}); | ||
} else if (target.isAttributeReference()) { | ||
var val = accessor.getRaw(target.name()); | ||
if (typeof val == 'number') { | ||
accessor.setRaw(target.name(), val + _this.value); | ||
} | ||
accessor.get(target.name()).mutate(function (value) { | ||
return value + _this.value; | ||
}); | ||
} else { | ||
throw new Error('Unable to apply to target ' + target.toString()); | ||
throw new Error("Unable to apply to target " + target.toString()); | ||
} | ||
@@ -42,0 +40,0 @@ }); |
@@ -15,4 +15,2 @@ 'use strict'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
@@ -38,4 +36,2 @@ | ||
value: function apply(targets, accessor) { | ||
var _this = this; | ||
if (!accessor.isIndexable()) { | ||
@@ -48,33 +44,11 @@ throw new Error('Attempt to apply insert patch to non-array value'); | ||
{ | ||
var _ret = function () { | ||
var pos = minIndex(targets, accessor); | ||
accessor.mutate(function (value) { | ||
if (value.length == 0 && pos == 0) { | ||
return _this.items; | ||
} | ||
return value.slice(0, pos).concat(_this.items).concat(value.slice(pos)); | ||
}); | ||
return { | ||
v: void 0 | ||
}; | ||
}(); | ||
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v; | ||
var pos = minIndex(targets, accessor); | ||
accessor.insert(pos, this.items); | ||
break; | ||
} | ||
case 'after': | ||
{ | ||
var _ret2 = function () { | ||
var pos = maxIndex(targets, accessor); | ||
accessor.mutate(function (value) { | ||
if (value.length == 0 && pos == 0) { | ||
return _this.items; | ||
} | ||
return value.slice(0, pos + 1).concat(_this.items).concat(value.slice(pos + 1)); | ||
}); | ||
return { | ||
v: void 0 | ||
}; | ||
}(); | ||
if ((typeof _ret2 === 'undefined' ? 'undefined' : _typeof(_ret2)) === "object") return _ret2.v; | ||
var _pos = maxIndex(targets, accessor); | ||
accessor.insert(_pos + 1, this.items); | ||
break; | ||
} | ||
@@ -81,0 +55,0 @@ default: |
@@ -29,3 +29,3 @@ "use strict"; | ||
} else if (target.isAttributeReference()) { | ||
if (!accessor.isSet(target.name())) { | ||
if (!accessor.has(target.name())) { | ||
accessor.setRaw(target.name(), _this.value); | ||
@@ -32,0 +32,0 @@ } |
@@ -24,23 +24,8 @@ 'use strict'; | ||
if (accessor.isIndexable()) { | ||
(function () { | ||
var indicies = (0, _util.targetsToIndicies)(targets, accessor); | ||
// TODO: Optimize to use slice operations | ||
accessor.mutate(function (value) { | ||
var length = value.length; | ||
var newValue = []; | ||
// Copy every value _not_ in the indicies array over to the newValue | ||
for (var i = 0; i < length; i++) { | ||
if (indicies.indexOf(i) == -1) { | ||
newValue.push(value[i]); | ||
} | ||
} | ||
return newValue; | ||
}); | ||
})(); | ||
var indicies = (0, _util.targetsToIndicies)(targets, accessor); | ||
// TODO: Optimize to use slice operations | ||
accessor.deleteIndicies(indicies); | ||
} else if (accessor.isPlainObject()) { | ||
accessor.mutate(function (value) { | ||
targets.forEach(function (target) { | ||
delete value[target.name()]; | ||
}); | ||
return value; | ||
targets.forEach(function (target) { | ||
accessor.delete(target.name()); | ||
}); | ||
@@ -47,0 +32,0 @@ } else { |
{ | ||
"name": "@sanity/mutator", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "A set of models to make it easier to utilize the powerful real time collaborative features of Sanity", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -17,10 +17,10 @@ import DMP from 'diff-match-patch' | ||
target.toIndicies(accessor).forEach(i => { | ||
const value = accessor.getIndexRaw(i) | ||
const newValue = dmp.patch_apply(this.dmpPatch, value)[0] | ||
accessor.setIndexRaw(i, newValue) | ||
accessor.getIndex(i).mutate(value => { | ||
return dmp.patch_apply(this.dmpPatch, value)[0] | ||
}) | ||
}) | ||
} else if (target.isAttributeReference()) { | ||
const value = accessor.getRaw(target.name()) | ||
const newValue = dmp.patch_apply(this.dmpPatch, value)[0] | ||
accessor.setRaw(target.name(), newValue) | ||
accessor.get(target.name()).mutate(value => { | ||
return dmp.patch_apply(this.dmpPatch, value)[0] | ||
}) | ||
} else { | ||
@@ -27,0 +27,0 @@ throw new Error(`Unable to apply diffMatchPatch to target ${target.toString()}`) |
@@ -86,7 +86,2 @@ // @flow | ||
} | ||
// The key exists and the value is not null | ||
isSet(key : string) : bool { | ||
const value = this.getRaw(key) | ||
return value !== null && typeof value != 'undefined' | ||
} | ||
getIndexRaw(i : number) : any { | ||
@@ -111,3 +106,37 @@ return this.getter()[i] | ||
} | ||
delete(key : string) { | ||
this._mutate(value => { | ||
delete value[key] | ||
return value | ||
}) | ||
} | ||
deleteIndicies(indicies : Array<number>) { | ||
this._mutate(value => { | ||
const length = value.length | ||
const newValue = [] | ||
// Copy every value _not_ in the indicies array over to the newValue | ||
for (let i = 0; i < length; i++) { | ||
if (indicies.indexOf(i) == -1) { | ||
newValue.push(value[i]) | ||
} | ||
} | ||
return newValue | ||
}) | ||
} | ||
insert(pos : number, items : Array<any>) { | ||
this._mutate(value => { | ||
if (this.getLength() == 0 && pos == 0) { | ||
return items | ||
} | ||
return value.slice(0, pos).concat(items).concat(value.slice(pos)) | ||
}) | ||
} | ||
mutate(fn : Function) { | ||
if (!this.isPrimitiveValue()) { | ||
throw new Error("Won't mutate container types") | ||
} | ||
this._mutate(fn) | ||
} | ||
// Not part of the Accessor protocol | ||
_mutate(fn : Function) { | ||
let val = this.getter() | ||
@@ -114,0 +143,0 @@ // Make sure we send a copy to the mutator |
@@ -12,12 +12,10 @@ export default class Set { | ||
target.toIndicies(accessor).forEach(i => { | ||
const val = accessor.getIndexRaw(i) | ||
if (typeof val == 'number') { | ||
accessor.setIndexRaw(i, val + this.value) | ||
} | ||
accessor.getIndex(i).mutate(value => { | ||
return value + this.value | ||
}) | ||
}) | ||
} else if (target.isAttributeReference()) { | ||
const val = accessor.getRaw(target.name()) | ||
if (typeof val == 'number') { | ||
accessor.setRaw(target.name(), val + this.value) | ||
} | ||
accessor.get(target.name()).mutate(value => { | ||
return value + this.value | ||
}) | ||
} else { | ||
@@ -24,0 +22,0 @@ throw new Error(`Unable to apply to target ${target.toString()}`) |
@@ -22,19 +22,9 @@ // @flow | ||
const pos = minIndex(targets, accessor) | ||
accessor.mutate(value => { | ||
if (value.length == 0 && pos == 0) { | ||
return this.items | ||
} | ||
return value.slice(0, pos).concat(this.items).concat(value.slice(pos)) | ||
}) | ||
return | ||
accessor.insert(pos, this.items) | ||
break | ||
} | ||
case 'after': { | ||
const pos = maxIndex(targets, accessor) | ||
accessor.mutate(value => { | ||
if (value.length == 0 && pos == 0) { | ||
return this.items | ||
} | ||
return value.slice(0, pos + 1).concat(this.items).concat(value.slice(pos + 1)) | ||
}) | ||
return | ||
accessor.insert(pos + 1, this.items) | ||
break | ||
} | ||
@@ -41,0 +31,0 @@ default: { |
@@ -14,3 +14,3 @@ export default class SetIfMissing { | ||
} else if (target.isAttributeReference()) { | ||
if (!accessor.isSet(target.name())) { | ||
if (!accessor.has(target.name())) { | ||
accessor.setRaw(target.name(), this.value) | ||
@@ -17,0 +17,0 @@ } |
@@ -13,19 +13,6 @@ import {targetsToIndicies} from './util' | ||
// TODO: Optimize to use slice operations | ||
accessor.mutate(value => { | ||
const length = value.length | ||
const newValue = [] | ||
// Copy every value _not_ in the indicies array over to the newValue | ||
for (let i = 0; i < length; i++) { | ||
if (indicies.indexOf(i) == -1) { | ||
newValue.push(value[i]) | ||
} | ||
} | ||
return newValue | ||
}) | ||
accessor.deleteIndicies(indicies) | ||
} else if (accessor.isPlainObject()) { | ||
accessor.mutate(value => { | ||
targets.forEach(target => { | ||
delete value[target.name()] | ||
}) | ||
return value | ||
targets.forEach(target => { | ||
accessor.delete(target.name()) | ||
}) | ||
@@ -37,2 +24,1 @@ } else { | ||
} | ||
@@ -35,2 +35,18 @@ /* eslint-disable id-length, quote-props */ | ||
}, | ||
{ | ||
name: 'Insert after empty', | ||
before: { | ||
a: [] | ||
}, | ||
patch: { | ||
insert: { | ||
after: 'a[-1]', | ||
items: [-1, -2] | ||
} | ||
}, | ||
after: { | ||
a: [-1, -2] | ||
} | ||
} | ||
] |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
85
6155
299293
1