lens.ts
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -10,8 +10,2 @@ export declare class Lens<T, U> { | ||
} | ||
export declare class KeyLens<T, K extends keyof T> extends Lens<T, T[K]> { | ||
constructor(key: K); | ||
} | ||
export declare class IndexLens<E> extends Lens<Array<E>, E> { | ||
constructor(idx: number); | ||
} | ||
export declare function lens<T>(): Lens<T, T>; |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var 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 function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -19,3 +9,3 @@ var Lens = /** @class */ (function () { | ||
Lens.prototype.k = function (key) { | ||
return this.compose(new KeyLens(key)); | ||
return this.compose(keyL(key)); | ||
}; | ||
@@ -34,23 +24,26 @@ Lens.prototype.compose = function (other) { | ||
Lens.prototype.i = function (idx) { | ||
return this.compose(new IndexLens(idx)); | ||
// implementation is the same as .k() | ||
return this.compose(keyL(idx)); | ||
}; | ||
var KeyLens = /** @class */ (function (_super) { | ||
__extends(KeyLens, _super); | ||
function KeyLens(key) { | ||
return _super.call(this, function (t) { return t[key]; }, function (v) { return function (t) { | ||
return Object.assign({}, t, (_a = {}, _a[key] = v, _a)); | ||
var _a; | ||
}; }) || this; | ||
function copy(x) { | ||
if (Array.isArray(x)) { | ||
return x.map(function (e) { return e; }); | ||
} | ||
return KeyLens; | ||
}(Lens)); | ||
exports.KeyLens = KeyLens; | ||
var IndexLens = /** @class */ (function (_super) { | ||
__extends(IndexLens, _super); | ||
function IndexLens(idx) { | ||
return _super.call(this, function (t) { return t[idx]; }, function (v) { return function (t) { return t.map(function (_v, _i) { return _i === idx ? v : _v; }); }; }) || this; | ||
else if (x && typeof x === 'object') { | ||
return Object.keys(x).reduce(function (res, k) { | ||
res[k] = x[k]; | ||
return res; | ||
}, {}); | ||
} | ||
return IndexLens; | ||
}(Lens)); | ||
exports.IndexLens = IndexLens; | ||
else { | ||
return x; | ||
} | ||
} | ||
function keyL(prop) { | ||
return new Lens(function (t) { return t[prop]; }, function (v) { return function (t) { | ||
var copied = copy(t); | ||
copied[prop] = v; | ||
return copied; | ||
}; }); | ||
} | ||
function lens() { | ||
@@ -57,0 +50,0 @@ return new Lens(function (t) { return t; }, function (v) { return function (t) { return v; }; }); |
{ | ||
"name": "lens.ts", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "TypeScript Lens implementation", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -83,8 +83,3 @@ # lens.ts [![travis-ci](https://travis-ci.org/utatti/lens.ts.svg?branch=master)](https://travis-ci.org/utatti/lens.ts) | ||
``` typescript | ||
import { | ||
lens, | ||
Lens, | ||
KeyLens, | ||
IndexLens | ||
} from 'lens.ts'; | ||
import { lens, Lens } from 'lens.ts'; | ||
``` | ||
@@ -106,2 +101,4 @@ | ||
Usually, you don't need to import `Lens` directly. | ||
``` typescript | ||
@@ -120,3 +117,3 @@ class Lens<T, U> { | ||
Retrives an actual value from an actual source. | ||
Retrive an actual value from an actual source. | ||
@@ -159,3 +156,3 @@ ``` typescript | ||
Composes 2 lenses into one. | ||
Compose 2 lenses into one. | ||
@@ -171,3 +168,3 @@ ``` typescript | ||
A helper method to narrow the lens for a property of `U`. | ||
Narrow the lens for a property of `U`. | ||
@@ -180,5 +177,2 @@ ``` typescript | ||
lens.k('name') // :: Lens<Person, string> | ||
// ↑ is a shortcut for ↓ | ||
lens.compose(new KeyLens<Person, 'name'>('name')) | ||
``` | ||
@@ -188,4 +182,4 @@ | ||
A helper method to narrow the lens for an element of an array `U`. A type error | ||
is thrown if `U` is not an array. | ||
Narrow the lens for an element of an array `U`. A type error is thrown if `U` is | ||
not an array. | ||
@@ -196,19 +190,6 @@ ``` typescript | ||
lens.i(10) // :: Lens<T, E> | ||
// ↑ is *roughly* a shortcut for ↓ | ||
lens.compose(new IndexLens<E>(10)) | ||
``` | ||
### `KeyLens`, `IndexLens` | ||
They are exported for the references of the `.k()` and `.i()` methods above, but | ||
it's not recommended using them by themselves. Please use the helper methods. | ||
``` typescript | ||
lens().k('name') // instead of .compose(new KeyLens('name')) | ||
lens().i(10) // instead of .compose(new IndexLens(10)) | ||
``` | ||
## License | ||
[MIT](LICENSE) |
@@ -9,3 +9,3 @@ export class Lens<T, U> { | ||
public k<K extends keyof U>(key: K): Lens<T, U[K]> { | ||
return this.compose(new KeyLens(key)); | ||
return this.compose(keyL(key)); | ||
} | ||
@@ -29,21 +29,28 @@ | ||
Lens.prototype.i = function(idx) { | ||
return this.compose(new IndexLens(idx)); | ||
// implementation is the same as .k() | ||
return this.compose(keyL(idx as any)); | ||
}; | ||
export class KeyLens<T, K extends keyof T> extends Lens<T, T[K]> { | ||
constructor(key: K) { | ||
super( | ||
t => t[key], | ||
v => t => Object.assign({}, t, { [key]: v }) | ||
); | ||
function copy<T>(x: T): T { | ||
if (Array.isArray(x)) { | ||
return x.map(e => e) as any; | ||
} else if (x && typeof x === 'object') { | ||
return Object.keys(x).reduce((res, k) => { | ||
res[k] = (x as any)[k]; | ||
return res; | ||
}, {} as any); | ||
} else { | ||
return x; | ||
} | ||
} | ||
export class IndexLens<E> extends Lens<Array<E>, E> { | ||
constructor(idx: number) { | ||
super( | ||
t => t[idx], | ||
v => t => t.map((_v, _i) => _i === idx ? v : _v) | ||
); | ||
} | ||
function keyL<T, K extends keyof T>(prop: K): Lens<T, T[K]> { | ||
return new Lens( | ||
t => t[prop], | ||
v => t => { | ||
const copied = copy(t); | ||
copied[prop] = v; | ||
return copied; | ||
} | ||
); | ||
} | ||
@@ -50,0 +57,0 @@ |
8876
110
188