Permalink
Browse files

init

  • Loading branch information...
vadimdemedes committed Jun 24, 2017
0 parents commit e1901dcdfda7527e36604461d1b168234f6542e1
Showing with 2,111 additions and 0 deletions.
  1. +12 βˆ’0 .editorconfig
  2. +2 βˆ’0 .gitattributes
  3. +1 βˆ’0 .gitignore
  4. +5 βˆ’0 .travis.yml
  5. +59 βˆ’0 index.js
  6. +56 βˆ’0 lib/component.js
  7. +39 βˆ’0 lib/components/colors.js
  8. +3 βˆ’0 lib/components/group.js
  9. +12 βˆ’0 lib/components/indent.js
  10. +5 βˆ’0 lib/components/newline.js
  11. +72 βˆ’0 lib/diff.js
  12. +38 βˆ’0 lib/h.js
  13. +138 βˆ’0 lib/instance.js
  14. +5 βˆ’0 lib/options.js
  15. +19 βˆ’0 lib/render-queue.js
  16. +31 βˆ’0 lib/render-to-string.js
  17. +11 βˆ’0 lib/string-component.js
  18. +11 βˆ’0 lib/transform-props.js
  19. +16 βˆ’0 lib/unmount.js
  20. +17 βˆ’0 lib/vnode.js
  21. +21 βˆ’0 license
  22. BIN media/demo.gif
  23. +77 βˆ’0 package.json
  24. +61 βˆ’0 readme.md
  25. +427 βˆ’0 test/components.js
  26. +130 βˆ’0 test/context.js
  27. +165 βˆ’0 test/h.js
  28. +612 βˆ’0 test/lifecycle.js
  29. +66 βˆ’0 test/refs.js
@@ -0,0 +1,12 @@
root = true
[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[{package.json,*.yml}]
indent_style = space
indent_size = 2
@@ -0,0 +1,2 @@
* text=auto
*.js text eol=lf
@@ -0,0 +1 @@
node_modules
@@ -0,0 +1,5 @@
language: node_js
node_js:
- '8'
- '6'
- '4'
@@ -0,0 +1,59 @@
'use strict';
const logUpdate = require('log-update');
const StringComponent = require('./lib/string-component');
const Component = require('./lib/component');
const renderToString = require('./lib/render-to-string');
const unmount = require('./lib/unmount');
const diff = require('./lib/diff');
const h = require('./lib/h');
const Newline = require('./lib/components/newline');
const Indent = require('./lib/components/indent');
const Group = require('./lib/components/group');
const Colors = require('./lib/components/colors');
exports.StringComponent = StringComponent;
exports.Component = Component;
exports.h = h;
exports.Newline = Newline;
exports.Indent = Indent;
exports.Group = Group;
Object.assign(exports, Colors);
const noop = () => {};
exports.diff = diff;
exports.renderToString = renderToString;
const render = (nextTree, prevTree, onUpdate = noop, context = {}) => {
return diff(prevTree, nextTree, onUpdate, context);
};
exports.render = render;
exports.mount = (tree, stream) => {
const log = logUpdate.create(stream || process.stdout);
let isUnmounted = false;
let currentTree;
const context = {};
const onUpdate = () => {
if (isUnmounted) {
return;
}
const nextTree = render(tree, currentTree, onUpdate, context);
log(renderToString(nextTree));
currentTree = nextTree;
};
currentTree = render(tree, null, onUpdate, context);
log(renderToString(currentTree));
return () => {
isUnmounted = true;
unmount(currentTree);
log.done();
};
};
@@ -0,0 +1,56 @@
'use strict';
const {enqueueUpdate} = require('./render-queue');
class Component {
constructor(props, context) {
this.props = props;
this.context = context;
this.state = {};
this._onUpdate = null;
this._pendingState = null;
}
setState(nextState) {
if (typeof nextState === 'function') {
nextState = nextState(this.state);
}
this._pendingState = Object.assign({}, this.state, nextState);
this._enqueueUpdate();
}
forceUpdate() {
this._enqueueUpdate();
}
componentWillMount() {}
componentDidMount() {}
componentWillUnmount() {}
componentDidUnmount() {}
componentWillReceiveProps() {}
componentWillUpdate() {}
componentDidUpdate() {}
shouldComponentUpdate() {
return true;
}
getChildContext() {
return {};
}
render() {
return null;
}
_render() {
return this.render(this.props, this.state, this.context);
}
_enqueueUpdate() {
enqueueUpdate(this._onUpdate);
}
}
module.exports = Component;
@@ -0,0 +1,39 @@
'use strict';
const chalk = require('chalk');
const StringComponent = require('../string-component');
const styles = [
'bold',
'dim',
'italic',
'underline',
'inverse',
'strikethrough',
'black',
'red',
'green',
'yellow',
'blue',
'magenta',
'cyan',
'white',
'gray',
'bgBlack',
'bgRed',
'bgGreen',
'bgYellow',
'bgBlue',
'bgMagenta',
'bgCyan',
'bgWhite'
];
styles.forEach(style => {
const capitalizedName = style[0].toUpperCase() + style.slice(1);
exports[capitalizedName] = class extends StringComponent {
renderString(children) {
return chalk[style](children);
}
};
});
@@ -0,0 +1,3 @@
'use strict';
module.exports = ({children}) => children;
@@ -0,0 +1,12 @@
'use strict';
const indentString = require('indent-string');
const StringComponent = require('../string-component');
class Indent extends StringComponent {
renderString(children) {
return indentString(children, this.props.size || 1, ' ');
}
}
module.exports = Indent;
@@ -0,0 +1,5 @@
'use strict';
const os = require('os');
module.exports = () => os.EOL;
@@ -0,0 +1,72 @@
'use strict';
const isEqual = require('lodash.isequal');
const {getInstance, getOrCreateInstance} = require('./instance');
const VNode = require('./vnode');
const diff = (prevNode, nextNode, onUpdate, context) => {
const prevInstance = getInstance(prevNode);
if (typeof nextNode === 'number') {
if (prevNode instanceof VNode) {
prevInstance.unmount();
}
return String(nextNode);
}
if (!nextNode || typeof nextNode === 'boolean') {
if (prevNode instanceof VNode) {
prevInstance.unmount();
}
return null;
}
if (typeof nextNode === 'string') {
return nextNode;
}
const nextInstance = getOrCreateInstance(nextNode);
nextInstance.context = context;
nextInstance.onUpdate = onUpdate;
if (!prevNode) {
nextInstance.mount();
return nextNode;
}
if (prevNode.component !== nextNode.component) {
prevInstance.unmount();
nextInstance.mount();
return nextNode;
}
const shouldUpdate = prevInstance.shouldComponentUpdate(nextNode.props);
const prevChildren = [].slice.call(prevNode.children);
if (!isEqual(prevNode.props, nextNode.props)) {
prevInstance.props = nextNode.props;
}
if (shouldUpdate) {
prevInstance.rerender();
}
const nextChildren = prevNode.children;
const length = Math.max(prevChildren.length, nextChildren.length);
const reconciledChildren = [];
for (let index = 0; index < length; index++) {
const childNode = diff(prevChildren[index], nextChildren[index], onUpdate, context);
reconciledChildren.push(childNode);
}
prevInstance.children = reconciledChildren;
return prevNode;
};
module.exports = diff;
@@ -0,0 +1,38 @@
'use strict';
const flatten = require('lodash.flattendeep');
const VNode = require('./vnode');
module.exports = (component, props, ...children) => {
props = props || {};
const readyChildren = [];
if (children.length > 0) {
props.children = children;
}
flatten(props.children).forEach(child => {
if (typeof child === 'number') {
child = String(child);
}
if (typeof child === 'boolean' || child === null) {
child = '';
}
if (typeof child === 'string') {
if (typeof readyChildren[readyChildren.length - 1] === 'string') {
readyChildren[readyChildren.length - 1] += child;
} else {
readyChildren.push(child);
}
} else {
readyChildren.push(child);
}
});
props.children = readyChildren;
return new VNode(component, props);
};
Oops, something went wrong.

0 comments on commit e1901dc

Please sign in to comment.