UNPKG

@typed-f/lens

Version:

[![NPM Version][lens-npm-version-badge]][lens-npm] [repo-circleci-badge]: https://img.shields.io/circleci/project/github/Ailrun/typed-f/master.svg?logo=circleci [![Known Vulnerabilities][lens-snyk-badge]][lens-snyk] [![Supported TypeScript Version][repo-s

128 lines (127 loc) 4.31 kB
"use strict"; 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 (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { 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 }); var Lens = /** @class */ (function () { function Lens(get, set) { this._get = get; this._set = set; } Lens.prototype.get = function () { return this._get; }; Lens.prototype.set = function () { return this._set; }; Lens.prototype.map = function () { var _this = this; return function (s) { return function (f) { return _this._set(s)(f(_this._get(s))); }; }; }; Lens.prototype.compose = function (other) { var _this = this; return new Lens(function (s) { return other.get()(_this._get(s)); }, function (s) { return function (bb) { return _this._set(s)((other.set()(_this._get(s))(bb))); }; }); }; return Lens; }()); exports.Lens = Lens; var LensS = /** @class */ (function (_super) { __extends(LensS, _super); function LensS(get, set) { return _super.call(this, get, set) || this; } LensS.prototype.makeInner = function (key) { return new LensGenerator().fromKey(key); }; LensS.prototype.focusTo = function (key) { var _this = this; return new LensS(function (s) { return _this._get(s)[key]; }, function (s) { return function (n) { var _a; var part = _this._get(s); if (part === undefined) { return _this._set(s)((_a = {}, _a[key] = n, _a)); } var newPart = copy(part); newPart[key] = n; return _this._set(s)(newPart); }; }); }; return LensS; }(Lens)); exports.LensS = LensS; var LensGenerator = /** @class */ (function () { function LensGenerator() { } LensGenerator.prototype.fromKey = function (key) { return new LensS(function (s) { return s[key]; }, function (s) { return function (b) { var _a; if (s === undefined) { return _a = {}, _a[key] = b, _a; } var res = copy(s); res[key] = b; return res; }; }); }; //tslint:enable: max-line-length LensGenerator.prototype.fromKeys = function () { var keys = []; for (var _i = 0; _i < arguments.length; _i++) { keys[_i] = arguments[_i]; } var rootLens = new LensS(function (s) { return s; }, function (_s) { return function (t) { return t; }; }); return keys.reduce(function (acc, key) { return acc.focusTo(key); }, rootLens); }; LensGenerator.prototype.byProxy = function () { var rootLens = new LensS(function (s) { return s; }, function (_s) { return function (t) { return t; }; }); return proxyfy(rootLens); }; return LensGenerator; }()); exports.LensGenerator = LensGenerator; function proxyfy(lens) { return new Proxy(lens, { get: function (target, prop) { if (target[prop] !== undefined) { return target[prop]; } return proxyfy(target.focusTo(prop)); }, }); } function copy(x) { if (Array.isArray(x)) { return x.slice(); } if (typeof x === 'object' && x !== null) { return __assign({}, x); } return x; }