Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix transitions of Microstate Arrays #127

Merged
merged 5 commits into from
Jun 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,25 @@
* [toggle() => microstate](#toggle--microstate)
* [`Number`](#number)
* [set(value: any) => microstate](#setvalue-any--microstate-1)
* [sum(number: Number [, number: Number]) => microstate](#sumnumber-number--number-number--microstate)
* [subtract(number: Number, [, number: Number]) => microstate](#subtractnumber-number--number-number--microstate)
* [increment(step: Number = 1) => microstate](#incrementstep-number--1--microstate)
* [decrement(step: Number = 1) => microstate](#decrementstep-number--1--microstate)
* [`String`](#string)
* [set(value: any) => microstate](#setvalue-any--microstate-2)
* [concat(str: String [, str1: String]) => microstate](#concatstr-string--str1-string--microstate)
* [`Array`](#array)
* [set(value: any) => microstate](#setvalue-any--microstate-3)
* [push(value: any [, value1: any]) => microstate](#pushvalue-any--value1-any--microstate)
* [push(value: any) => microstate](#pushvalue-any--value1-any--microstate)
* [pop() => microstate](#pop--microstate)
* [shift() => microstate](#shift--microstate)
* [unshift(value: any) => microstate](#unshift-any--value1-any--microstate)
* [filter(fn: value => boolean) => microstate](#filterfn-value--boolean--microstate)
* [map(fn: (value, index) => any) => microstate](#mapfn-value-index--any--microstate)
* [replace(item: any, replacement; any) => microstate](#replaceitem-any-replacement-any--microstate)
* [map(fn: (microstate, index) => any) => microstate](#mapfn-microstate-index--any--microstate)
* [clear() => microstate](#clear--microstate)
* [`Object`](#object)
* [set(value: any) => microstate](#setvalue-any--microstate-4)
* [assign(object: Object) => microstate](#assignobject-object--microstate)
* [put(key: string, value: any) => microstate]()
* [delete(key: string) => microstate]()
* [FAQ](#faq)
* [What if I can't use class syntax?](#what-if-i-cant-use-class-syntax)
* [What if I can't use Class Properties?](#what-if-i-cant-use-class-properties)
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
"dependencies": {
"funcadelic": "0.5.0",
"get-prototype-descriptors": "0.6.0",
"invariant": "2.2.4",
"memoize-getters": "1.1.0",
"ramda": "^0.25.0",
"symbol-observable": "1.2.0"
},
Expand Down
1 change: 1 addition & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const globals = {
"ramda/src/lensPath": "R.lensPath",
"symbol-observable": "SymbolObservable",
"get-prototype-descriptors": "getPrototypeDescriptors",
"invariant": "invariant"
};

module.exports = {
Expand Down
19 changes: 19 additions & 0 deletions src/transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { flatMap, map } from 'funcadelic';
import { params } from './types/parameters0';

export default function transform(fn, microstate) {
return map(tree => flatMap(current => {
if (current.is(tree)) {
return current.assign({
meta: {
children() {
let { T } = params(current.Type);
return fn(current.children, T);
}
}
})
} else {
return current;
}
}, tree), microstate);
}
7 changes: 6 additions & 1 deletion src/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import types, { params, toType } from './types';
import $ from './utils/chain';
import { keep, reveal } from './utils/secret';
import values from './values';
import invariant from 'invariant';

const { assign, defineProperties } = Object;

Expand Down Expand Up @@ -336,7 +337,11 @@ export default class Tree {
* the place where the branch ends is the focus point.
*/
get lens() {
let get = tree => tree.treeAt(this.path).prune()
let get = tree => {
let found = tree.treeAt(this.path);
invariant(found instanceof Tree, `Tree at path [${this.path.join(', ')}] does not exist. Is path wrong?`);
return found.prune();
}

let set = (tree, root) => {
let nextValue = lset(lensPath(this.path), tree.value, root.value);
Expand Down
129 changes: 91 additions & 38 deletions src/types/array.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,125 @@
import { append, map, flatMap } from 'funcadelic';
import { append, map } from 'funcadelic';
import transform from '../transform';
import Tree from '../tree';
import { parameterized, params } from './parameters0';
import Any from './any';

import { parameterized } from './parameters0';
class ArrayType {
initialize(value = []) {
return value;
}

/**
* The push() transition adds one element to the end of the array.
* Returns the next microstate.
* @param {*} value
* @returns {Microstate}
*/
push(value) {
return transform((children, T) => append(children, new Tree({ Type: T, value })), this);
return transform((children, T) => {
return append(children, Tree.from(value, T).graft([children.length]));
}, this);
}

/**
* The pop() transition removes the last element from an array.
* Returns the next microstate.
* @returns {Microstate}
*/
pop() {
return transform(children => children.slice(0, -1), this);
}

/**
* The shift() transition removes the first element from an array.
* Returns the next microstate.
* @returns {Microstate}
*/
shift() {
return transform(children => children.slice(1), this);
return transform(children => {
return map((shifted, index) => {
return map(tree => {
let [, ...rest] = tree.path;
return tree.assign({
meta: {
path: [index, ...rest]
}
})
}, shifted);
}, children.slice(1));
}, this);
}

/**
* The unshift() transition adds one element to the beginning of an array.
* Returns the next microstate.
* @returns {Microstate}
*/
unshift(value) {
return transform((children, T) => append([new Tree({ Type: T, value })], children), this);
return transform((children, T) => {
return append([Tree.from(value, T).graft([0])], map((child, index) => {
return map(tree => {
let [, ...rest] = tree.path;
return tree.assign({
meta: {
path: [index + 1, ...rest]
}
});
}, child);
}, children))
}, this);
}

/**
* The filter() transition creates a new array with all elements
* that pass the test implemented by the provided function.
* Returns the next microstate.
* @param {*} fn
* @returns {Microstate}
*/
filter(fn) {
return transform(children => children.filter(tree => fn(tree.state)), this);
return transform(children => {
return map((child, index) => {
return map(tree => {
let [, ...rest] = tree.path;
return tree.assign({
meta: {
path: [index, ...rest]
}
})
}, child);
}, children.filter(tree => fn(tree.state)));
}, this);
}

/**
* The map() transition creates a new array with the results of calling
* a provided function on every element in the calling array.
* Returns the next microstate.
* @param {*} fn
* @returns {Microstate}
*/
map(fn) {
return transform(children => {
return children.map(tree => {
let value = fn(tree.state);
if (value === tree.state) {
return transform((children, T) => {
return map((tree, index) => {
let { microstate } = tree.prune();
let mapped = Tree.from(fn(microstate, index), T);
if (tree.isEqual(mapped)) {
return tree;
} else {
return tree.assign({
data: {
value
}
});
return mapped.graft([index]);
}
})
}, children);
}, this);
}

splice(startIndex, length, value) {
return transform((children, T) => {
return children.splice(startIndex, length, new Tree({ Type: T, value}));
});
/**
* This clear() transition replaces the array with an empty array.
* Returns the next microstate.
* @returns {Microstate}
*/
clear() {
return this.set([]);
}
}

function transform(fn, microstate) {
return map(tree => flatMap(current => {
if (current.is(tree)) {
return current.assign({
meta: {
children() {
let { T } = params(current.Type);
return fn(current.children.slice(), T);
}
}
})
} else {
return current;
}
}, tree), microstate);
}

export default parameterized(ArrayType, {T: Any});
24 changes: 4 additions & 20 deletions src/types/object.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { map, filter, flatMap, foldl } from 'funcadelic';
import { parameterized, params } from './parameters0';
import Any from './any';
import { filter, foldl } from 'funcadelic';
import transform from '../transform';
import Tree from '../tree';
import Any from './any';
import { parameterized } from './parameters0';

const { assign, keys } = Object;

Expand Down Expand Up @@ -42,21 +43,4 @@ class ObjectType {
}
}

function transform(fn, microstate) {
return map(tree => flatMap(current => {
if (current.is(tree)) {
return current.assign({
meta: {
children() {
let { T } = params(current.Type);
return fn(current.children, T);
}
}
});
} else {
return current;
}
}, tree), microstate);
}

export default parameterized(ObjectType, {T: Any});