nicescript
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -12,2 +12,4 @@ const isProto = def(nice, 'isProto', {}), { Check } = nice; | ||
equal: (a, b) => a === b || (a && a.getResult ? a.getResult() : a) === (b && b.getResult ? b.getResult() : b), | ||
true: v => v === true, | ||
false: v => v === false, | ||
any: (v, ...vs) => vs.includes(v), | ||
@@ -53,4 +55,4 @@ array: a => Array.isArray(a), | ||
: v => v && v.constructor && v.constructor.name === nice.jsTypes[i].jsName); | ||
nice._on('Type', function defineReducer(type) { | ||
@@ -57,0 +59,0 @@ type.title && Check(type.title, v => { |
{ | ||
"name": "nicescript", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"keywords": [ | ||
@@ -5,0 +5,0 @@ "util", |
158
README.md
NiceScript | ||
========= | ||
A naive attempt to simplify life of a fellow JavaScript programmer. | ||
[![Build Status](https://travis-ci.org/nicescript/nicescript.svg?branch=master)](https://travis-ci.org/nicescript/nicescript) | ||
A naive attempt to simplify life of a fellow JavaScript programmer. | ||
Example ( [JS Bin](https://jsbin.com/kenedihasi/edit?html,output) ) | ||
```javascript | ||
const { Box, Div, B, Switch } = nice; | ||
const tasks = Box(['Feed the fish', 'Buy milk']); | ||
const decorate = Switch | ||
.equal('Watch tv')('Read book') | ||
.match(/buy/i).use(s => [s, B(' $').color('#3A3')]); | ||
const taskView = t => Div(t) | ||
.margin('1em 0') | ||
.padding('.5em') | ||
.borderRadius('.5em') | ||
.backgroundColor('#DEF'); | ||
Box.use(tasks).by(ts => Div(ts.map(decorate).map(taskView))).show(); | ||
tasks.push('Walk the dog', 'Watch tv'); | ||
``` | ||
More examples: | ||
* [Ball game](./examples/ball.html) ( [JS Bin](https://jsbin.com/wimayanovu/1/edit?html,output) ) | ||
* [Todo list](./examples/todo.html) ( [JS Bin](https://jsbin.com/yetufekopi/1/edit?html,output) ) | ||
## Install | ||
@@ -18,8 +45,7 @@ `npm install nicescript` | ||
`<script src="https://cdn.jsdelivr.net/npm/nicescript/nice.js"></script>` | ||
`<script src="https://unpkg.com/nicescript/nice.js"></script>` | ||
## Examples | ||
or | ||
* [Ball game](./examples/ball.html) | ||
* [Todo list](./examples/todo.html) | ||
`<script src="https://cdn.jsdelivr.net/npm/nicescript/nice.js"></script>` | ||
@@ -33,13 +59,13 @@ ## Tests | ||
* [Types](#types) - sometimes they useful. | ||
* [Types](#types) | ||
* [Functions](#functions) - adds couple features to regular JS functions. | ||
* [Switch](#switch) - finally convenient. | ||
* [Boxes](#boxes) - to handle data flow. | ||
* [Tag](#tag) - use all above to to create dom/html UI. | ||
* [Boxes](#boxes) - to handle state changes. | ||
* [Tag](#tag) - use all above to to create html UI. | ||
### Values | ||
### Nice values | ||
#### Single values | ||
```javascript | ||
const n = nice.Number(5); | ||
const n = nice(5); | ||
@@ -67,19 +93,6 @@ //read value | ||
#### Wrapping values | ||
Call nice with value to wrap it with most appropriate type. | ||
```javascript | ||
const nice = require('nicescript')(); | ||
nice(4); //nice.Number; | ||
nice(""); //nice.String; | ||
nice(true); //nice.Boolean; | ||
nice({}); //nice.Object; | ||
nice([]); //nice.Array; | ||
nice(1, 2, 3); //nice.Array; | ||
nice(null); //nice.Null; | ||
``` | ||
### Types | ||
Each value in NiceScript has a type. Here is a root of types hierarchy: | ||
#### Predefined types | ||
+ Anything | ||
@@ -106,2 +119,17 @@ + Something | ||
#### Wrapping values | ||
Call nice with js value to wrap it with most appropriate type. | ||
```javascript | ||
const nice = require('nicescript')(); | ||
nice(4); // nice.Number; | ||
nice(""); // nice.String; | ||
nice(true); // nice.Boolean; | ||
nice({}); // nice.Object; | ||
nice([]); // nice.Array; | ||
nice(1, 2, 3); // nice.Array; | ||
nice(null); // nice.Null; | ||
``` | ||
#### User types | ||
@@ -114,6 +142,7 @@ ```javascript | ||
let d = nice.Dog('Jim'); | ||
let d = nice.Dog('Jim').weight(5); | ||
d.name(); // Jim | ||
d.weight(); // 5 | ||
//by default created type extends nice.Object | ||
// by default created type extends nice.Object | ||
d.is.Object() // true | ||
@@ -128,23 +157,23 @@ | ||
```javascript | ||
//Anonymous | ||
// Creating anonymous function | ||
const f = nice.Function(n => n + 1); | ||
f(1); //2 | ||
f(1); // 2 | ||
//Named functions will be added to nice | ||
// Named functions will be added to nice | ||
const plusTwo = nice.Function('plusTwo', n => n + 2); | ||
//or nice.Function(function plusTwo(n) { return n + 2; }); | ||
plusTwo(1); //3 | ||
nice.plusTwo(1); //3 | ||
plusTwo(1); // 3 | ||
nice.plusTwo(1); // 3 | ||
//Check argument type | ||
// Check argument type | ||
const x2 = nice.Function.number('x2', n => n * 2); | ||
x2(21); //42 | ||
nice.x2(21); //42 | ||
nice.Number(1).x2();//42 | ||
x2('q'); //throws "Function can't handle (String)" | ||
x2(21); // 42 | ||
nice.x2(21); // 42 | ||
nice.Number(1).x2();// 42 | ||
x2('q'); // throws "Function can't handle (String)" | ||
//overload | ||
// now let's overload x2 for strings | ||
x2.string(s => s + '!'); | ||
x2(21); //42 | ||
x2('q'); //q! | ||
x2(21); // 42 | ||
x2('q'); // q! | ||
@@ -169,8 +198,8 @@ ``` | ||
##### Check | ||
Returns boolean. Never chenges it's arguments. | ||
Returns boolean. Never changes it's arguments. | ||
After definition named Check can be used in [Switch](#switch) and 'is' statements. | ||
##### Action | ||
Changes first argument. Each body of Action should return nice.Ok or nice.Fail | ||
or throw exception. Function returns it's first argument. | ||
Changes first argument. Action always returns it's first argument so you can | ||
call multiple actions in a row. | ||
@@ -185,5 +214,2 @@ ```javascript | ||
### Interfaces | ||
### Switch | ||
@@ -198,9 +224,9 @@ Delayed argumet | ||
.default(42); | ||
f(1); //11 | ||
f(3); //22 | ||
f('qwe'); //"qwe!" | ||
f([]); //42 | ||
f(0); //42 | ||
f(undefined); //:( | ||
f(null); //:( | ||
f(1); // 11 | ||
f(3); // 22 | ||
f('qwe'); // "qwe!" | ||
f([]); // 42 | ||
f(0); // 42 | ||
f(undefined); // :( | ||
f(null); // :( | ||
``` | ||
@@ -225,4 +251,4 @@ Instant argument | ||
```javascript | ||
nice.Function.Nothing(() => 1).Null(() => 2)(null); //2 | ||
nice.Switch.Nothing.use(() => 1).Null.use(() => 2)(null); //1 | ||
nice.Function.Nothing(() => 1).Null(() => 2)(null); // 2 | ||
nice.Switch.Nothing.use(() => 1).Null.use(() => 2)(null); // 1 | ||
``` | ||
@@ -242,7 +268,7 @@ Besides current implementation of Switch use only first argument. | ||
//create Box that follows changes in b | ||
// create Box that follows changes in b | ||
let b2 = Box.use(b).by(n => n * 2); | ||
b(3); // b2() === 6 | ||
//Named inputs | ||
// Named inputs | ||
let square = Box() | ||
@@ -257,3 +283,19 @@ .Number('x', 5) | ||
Calling [mapping](#mapping) on box will create new box that follows changes in the original. | ||
```javascript | ||
const a = nice.Box('qwe'); | ||
const b = a.concat('!').listen(console.log); | ||
// qwe! | ||
a('asd'); | ||
// asd! | ||
``` | ||
Calling [action](#action) on box will change its content. | ||
```javascript | ||
const a = nice.Box([1, 2]).listen(console.log); | ||
[1, 2]; | ||
a.push(3); | ||
[1, 2, 3]; | ||
``` | ||
### Tag | ||
@@ -268,7 +310,7 @@ ```javascript | ||
//browser and server | ||
// browser and server | ||
div.html | ||
// <div style="margin:10px;font-size:20px">Normal text <i>italic </i>normal <b style="color:red">red bold</b></div> | ||
//browser only | ||
// browser only | ||
div.show(); // attach dom node to document.body or provided node | ||
@@ -275,0 +317,0 @@ ``` |
@@ -42,2 +42,8 @@ def(nice, 'Block', (name, by) => { | ||
children.forEach(c => { | ||
if(is.Array(c)) | ||
return c.each(_c => z.add(_c)); | ||
if(is.array(c)) | ||
return _each(c, _c => z.add(_c)); | ||
if(c === undefined || c === null) | ||
@@ -154,3 +160,3 @@ return; | ||
}) | ||
.default.use((v, k, node) => node.style[k] = ''); | ||
.default.use((v, k, node) => node.style && (node.style[k] = '')); | ||
@@ -157,0 +163,0 @@ |
'Div,I,B,Span,H1,H2,H3,H4,H5,H6,P,LI,UL,OL'.split(',').forEach(t => | ||
nice.Block(t, (z, c, ...cs) => z.tag(t.toLowerCase()).add(c, ...cs))); | ||
nice.Block(t, (z, ...cs) => z.tag(t.toLowerCase()).add(...cs))); | ||
@@ -4,0 +4,0 @@ nice.Block('A', (z, url, ...children) => { |
@@ -54,2 +54,13 @@ const nice = require('../index.js')(); | ||
it("delayed equal with default", function() { | ||
let s = Switch | ||
.equal(true)(2) | ||
.default(nice.NOTHING); | ||
expect(s(true)).to.equal(2); | ||
// expect(s('asd')).to.equal(nice.NOTHING); | ||
}); | ||
it("switch check", function() { | ||
@@ -56,0 +67,0 @@ var spy1 = chai.spy(); |
@@ -29,2 +29,11 @@ let nice = require('../index.js')(); | ||
it("children array", function() { | ||
let div = nice.Div(['qwe', 'asd']); | ||
expect(div.html).to.equal('<div>qweasd</div>'); | ||
let div2 = nice.Div(nice('qwe', 'asd')); | ||
expect(div2.html).to.equal('<div>qweasd</div>'); | ||
}); | ||
it("item child", function() { | ||
@@ -31,0 +40,0 @@ let n = nice.Number(5); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
269371
8703
328