tsminilens
Version:
mini Lens for TypeScript
171 lines (170 loc) • 6.37 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.L = exports.lensFrom = exports.lensFor = exports.chain = exports.ChainedLens = exports.castIf = exports.CastLens = exports.SimpleLens = exports.Lens = void 0;
function mapNullable(f, n) { return n == null ? n : f(n); }
var Lens = /** @class */ (function () {
function Lens() {
}
Lens.prototype.set = function (obj, val) {
return this.over(obj, function (_) { return val; });
};
Lens.prototype.castIf = function (predicate) {
return castIf(this, predicate);
};
Lens.prototype.chain = function (lens1) {
return chain(this, lens1);
};
Object.defineProperty(Lens.prototype, "then", {
/*
chain lenses in a slightly more fluent way than chain()
*/
get: function () {
var _this = this;
return {
withPath: function () {
var ps = [];
for (var _i = 0; _i < arguments.length; _i++) {
ps[_i] = arguments[_i];
}
return chain(_this, new SimpleLens(ps));
},
to: function () {
var ps = [];
for (var _i = 0; _i < arguments.length; _i++) {
ps[_i] = arguments[_i];
}
return chain(_this, new SimpleLens(ps));
},
};
},
enumerable: false,
configurable: true
});
return Lens;
}());
exports.Lens = Lens;
var SimpleLens = /** @class */ (function (_super) {
__extends(SimpleLens, _super);
function SimpleLens(fields) {
var _this = _super.call(this) || this;
_this.fields = fields;
return _this;
}
SimpleLens.prototype.view = function (obj) {
return this.fields.reduce(function (st, cur) { return mapNullable(function (_) { return st[cur]; }, st); }, obj);
};
SimpleLens.prototype.set = function (obj, val) {
return this.over(obj, function (_) { return val; });
};
SimpleLens.prototype.over = function (obj, func) {
var setArrayAware = function (o, fld, val) {
var _a;
return o instanceof Array
? o.map(function (v, idx) { return idx === fld ? val : v; })
: __assign(__assign({}, o), (_a = {}, _a[fld] = val, _a));
};
return this.fields.reduceRight(function (st, cur) { return function (next) { return mapNullable(function (_) { return setArrayAware(next, cur, st(next[cur])); }, next); }; }, func)(obj);
};
return SimpleLens;
}(Lens));
exports.SimpleLens = SimpleLens;
var CastLens = /** @class */ (function (_super) {
__extends(CastLens, _super);
function CastLens(inner, predicate) {
var _this = _super.call(this) || this;
_this.inner = inner;
_this.predicate = predicate;
return _this;
}
CastLens.prototype.view = function (obj) {
var _this = this;
return mapNullable(function (v) { return _this.predicate(v) ? v : undefined; }, this.inner.view(obj));
};
CastLens.prototype.set = function (obj, val) {
return this.over(obj, function (_) { return val; });
};
CastLens.prototype.over = function (obj, func) {
var _this = this;
// over/set should allow acting on null, or null prevents set / override from happening
// because it won't satisfy the predicate
return this.inner.over(obj, function (v) { return null == v || _this.predicate(v) ? func(v) : v; });
};
return CastLens;
}(Lens));
exports.CastLens = CastLens;
function castIf(lens, predicate) {
return new CastLens(lens, predicate);
}
exports.castIf = castIf;
var ChainedLens = /** @class */ (function (_super) {
__extends(ChainedLens, _super);
function ChainedLens(inner1, inner2) {
var _this = _super.call(this) || this;
_this.inner1 = inner1;
_this.inner2 = inner2;
return _this;
}
ChainedLens.prototype.view = function (obj) {
var _this = this;
return mapNullable(function (v) { return _this.inner2.view(v); }, this.inner1.view(obj));
};
ChainedLens.prototype.set = function (obj, val) {
return this.over(obj, function (_) { return val; });
};
ChainedLens.prototype.over = function (obj, func) {
var _this = this;
return this.inner1.over(obj, function (fld) { return _this.inner2.over(fld, func); });
};
return ChainedLens;
}(Lens));
exports.ChainedLens = ChainedLens;
function chain(lens1, lens2) {
return new ChainedLens(lens1, lens2);
}
exports.chain = chain;
function lensFor() {
return {
withPath: function () {
var ps = [];
for (var _i = 0; _i < arguments.length; _i++) {
ps[_i] = arguments[_i];
}
return new SimpleLens(ps);
}
};
}
exports.lensFor = lensFor;
// alias for lensFor<T>().withPath()
function lensFrom() {
return { to: lensFor().withPath };
}
exports.lensFrom = lensFrom;
// an even terser alias
function L() { return lensFrom(); }
exports.L = L;