Skip to content

Commit

Permalink
just sketching some things
Browse files Browse the repository at this point in the history
  • Loading branch information
jdeal committed Nov 20, 2016
0 parents commit 29002a7
Show file tree
Hide file tree
Showing 22 changed files with 889 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .babelrc
@@ -0,0 +1,3 @@
{
"presets": ["es2015", "stage-2"]
}
87 changes: 87 additions & 0 deletions .eslintrc
@@ -0,0 +1,87 @@
{
"extends": "eslint:recommended",
"env": {
"node": true,
"browser": true
},
"rules": {
"quotes": [2, "single", "avoid-escape"],
"strict": [2, "never"],
"camelcase": 0,
"no-underscore-dangle": 0,
"new-cap": 0,
"indent": [2, 2],
"jsx-quotes": [2, "prefer-double"],

"comma-dangle": 0,
"comma-spacing": 2,
"consistent-return": 2,
"curly": [2, "all"],
"dot-notation": [2, { "allowKeywords": true }],
"eol-last": 2,
"eqeqeq": [2, "smart"],
"key-spacing": [2, { "beforeColon": false, "afterColon": true }],
"keyword-spacing": 2,
"new-parens": 2,
"no-alert": 2,
"no-array-constructor": 2,
"no-caller": 2,
"no-console": [2, { "allow": ["warn", "error"]}],
"no-delete-var": 2,
"no-eval": 2,
"no-extend-native": 2,
"no-extra-bind": 2,
"no-fallthrough": 2,
"no-implied-eval": 2,
"no-iterator": 2,
"no-label-var": 2,
"no-labels": 2,
"no-lone-blocks": 2,
"no-loop-func": 2,
"no-mixed-spaces-and-tabs": [2, false],
"no-multi-spaces": 2,
"no-multi-str": 2,
"no-native-reassign": 2,
"no-new": 2,
"no-new-func": 2,
"no-new-object": 2,
"no-new-wrappers": 2,
"no-octal": 2,
"no-octal-escape": 2,
"no-process-exit": 2,
"no-proto": 2,
"no-redeclare": 2,
"no-return-assign": 2,
"no-script-url": 2,
"no-sequences": 2,
"no-shadow": 2,
"no-shadow-restricted-names": 2,
"no-spaced-func": 2,
"no-trailing-spaces": 2,
"no-undef": 2,
"no-undef-init": 2,
"no-unused-expressions": 2,
"no-unused-vars": [2, {"vars": "all", "args": "after-used"}],
"no-use-before-define": 2,
"no-with": 2,
"semi": 2,
"semi-spacing": [2, {"before": false, "after": true}],
"space-infix-ops": 2,
"space-unary-ops": [2, {"words": true, "nonwords": false}],
"yoda": [2, "never"]
},
"globals": {
"escape": true,
"unescape": true,
"Promise": true
},
"plugins": [
],
"parserOptions": {
"ecmaVersion": 7,
"sourceType": "module",
"ecmaFeatures": {
"experimentalObjectRestSpread": true
}
},
}
31 changes: 31 additions & 0 deletions .gitignore
@@ -0,0 +1,31 @@
# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Dependency directory
/node_modules

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history

# Build directory
/lib
/react-redux-element-portal/lib
/react-redux-element-portal/package.json
/react-redux-element-portal/node_modules
/test/helpers/ReduxElementPortal.js
152 changes: 152 additions & 0 deletions README.md
@@ -0,0 +1,152 @@
```js
import im from 'im-js';

const obj = {
x: 1
};

const newObj = im.set(obj, 'x', 2);
```

```js
import im from 'im-js';

const array = [
1
];

const newArray = im.push(array, )
```

```js
im(obj).at('x').push(2).val();
```

```js
update(obj, {
x: {
$push: 2
}
})
```

```js
im.withMutations(flow, m => {
m('openFlowIds').push(String(flowId));
m.
})
```

```js
im.mutate(flow, m => {
m.mutate(thing)
})
```

obj.x.push(2)

obj.at('x').push(2).val()

im.pushAt('x', 2);

obj.x = 2

im.set(obj, 'x', 2);

im.push(obj, 'x', 2);

im.at('x').push(2)

obj.x.push(2)
im(array).push(2)
im(obj).at('x').push(2)
im(obj, 'x').push(2)


im(obj).at('array').push(2)

im(obj,
['set', 'x', 2],
['push', ]
)



const updatedFlow = update(flow, {
needsMeta: {
[nodeId]: {
[fieldKey]: {
isLoading: {$set: true},
loadStamp: {$set: timestampId},
choices: {$set: []},
selectedChoice: {$set: currentChoice}
}
}
}
});

const updatedFlow = {
...flow,
needsMeta: {
...flow.needsMeta,
[nodeId]: {
...flow.needsMeta[nodeId],
[fieldKey]: {
...flow.needsMeta[nodeId][fieldKey],
isLoading: true,
loadStamp: timestampId,
choices: [],
selectedChoice: currentChoice
}
}
}
}

flow.needsMeta[nodeId][fieldKey].isLoading = true;
flow.needsMeta[nodeId][fieldKey].loadStamp = timestampId;
flow.needsMeta[nodeId][fieldKey].choices = [];
flow.needsMeta[nodeId][fieldKey].selectedChoice = currentChoice;

_.extend(flow.needsMeta[nodeId][fieldKey], {
isLoading: true,
loadStamp: timestampId,
choices: [],
selectedChoice: currentChoice
});

im.extend(flow.needsMeta[nodeId][fieldKey], {
isLoading: true,
loadStamp: timestampId,
choices: [],
selectedChoice: currentChoice
})

return update(state, {
openFlowIds: {
$push: [String(flowId)]
}
});

im.push(state, 'openFlowIds', String(flowId))

im(state,
'openFlowIds', m => m.push(String(flowId))
)

im(flow,
['needsMeta', nodeId, fieldKey], () =>
)

im(state, 'openFlowIds').push(String(flowId))

im(state.openFlowIds).push(String(flowId))

im(flow).set('isLoaded', true);

im(state.openFlowIds, () => {

});

im(state, {
openFlowIds: $ => $.push(String(flowId))
})
Empty file added index.js
Empty file.
29 changes: 29 additions & 0 deletions package.json
@@ -0,0 +1,29 @@
{
"name": "im-js",
"version": "0.0.0",
"description": "Immutable helpers for plain JS objects and arrays",
"main": "index.js",
"scripts": {
"test": "ava test"
},
"keywords": [
"immutable"
],
"author": "Justin Deal <justin.deal@gmail.com>",
"license": "MIT",
"devDependencies": {
"ava": "^0.16.0",
"babel-cli": "^6.18.0",
"babel-core": "^6.18.0",
"babel-preset-es2015": "^6.18.0",
"babel-preset-stage-2": "^6.18.0",
"babel-register": "^6.18.0",
"eslint": "^3.9.0",
"sinon": "^1.17.6"
},
"dependencies": {
"invariant": "^2.2.1",
"object-assign": "^4.1.0",
"objectpath": "^1.2.1"
}
}
79 changes: 79 additions & 0 deletions src/createMutable.js
@@ -0,0 +1,79 @@
import objectAssign from 'object-assign';

class Mutable {

constructor(obj, parent, key) {
if (obj instanceof Mutable) {
return obj;
}
this._orig = obj;
this._obj = obj;
this._isObject = obj && typeof obj === 'object';
this._isArray = Array.isArray(obj);
this._parent = parent;
this._key = key;
this._children = {};
return this;
}

_detach() {
this._parent = undefined;
this._key = undefined;
}

get(key) {
if (!this._isObject) {
return undefined;
}
const child = this._obj[key];
const isChildObject = child && typeof child === 'object';
if (!isChildObject) {
return child;
}
if (!this._children[key]) {
this._children[key] = new Mutable(child, this, key);
}
return this._children[key];
}

set(key, value) {
value = value instanceof Mutable ? value._obj : value;
if (!this._isObject) {
return this;
}
if (this._obj[key] === value) {
return this;
}
if (this._obj === this._orig) {
if (this._isArray) {
this._obj = this._obj.slice(0);
} else {
this._obj = objectAssign({}, this._obj);
}
}
this._obj[key] = value;
if (this._parent) {
this._parent.set(this._key, this._obj);
}
if (this._children[key]) {
this._children[key]._detach();
this._children[key] = undefined;
}
return this;
}

value() {
return this._obj;
}

}

export const isMutable = (object) => {
return object instanceof Mutable;
};

const createMutable = obj => {
return new Mutable(obj);
};

export default createMutable;

0 comments on commit 29002a7

Please sign in to comment.