New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

nicescript

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nicescript

Reactive JavaScript library designed to reduce amount of boilerplate code on client and server.

  • 0.2.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
decreased by-80%
Maintainers
1
Weekly downloads
 
Created
Source

NiceScript

A naive attempt to simplify life of a fellow JavaScript programmer.

Build Status

Install

npm install nicescript

Then in node.js script:

const nice = require('nicescript')();

Browser:

<script src="https://cdn.jsdelivr.net/npm/nicescript/nice.js"></script>

Examples

Tests

npm test

Basic features

  • Types - sometimes they useful.
  • Functions - adds couple features to regular JS functions.
  • Switch - finally convenient.
  • Boxes - to handle data flow.
  • Tag - use all above to to create dom/html UI.

Values

Single values
const n = nice.Number(5);

//read value
n();      // 5

//write value
n(6);     // n
n();      // 6
Object values
const o = nice.Object({ a: 1 });

//get value
o('a');         // 1
o('b');         // nice.NOT_FOUND

//set value
o('b', 5);      // o
o('b');         // 5
Wrapping values

Call nice with value to wrap it with most appropriate type.

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

Predefined types
  • Anything
    • Something
    • Nothing
      • Error
      • Undefined
      • Null
      • NotFound
      • Fail
      • ...
User types
nice.Type('Dog')
  .String('title')
  .Number('weight')
  .by((z, title) => z.title(title));

let d = nice.Dog('Jim');
d.name();       // Jim 

//by default created type extends nice.Object
d.is.Object()   // true

Type name should start with capital letter.

Functions

//Anonymous
const f = nice.Function(n => n + 1);
f(1);               //2

//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

//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)"

//overload
x2.string(s => s + '!');
x2(21);             //42
x2('q');            //q!

Function name should start with lowercase letter.

Function types
Mapping

Clean function that never changes it's arguments. NiceScript will always wrap result of Mapping.

nice.Mapping.Number.Number('times', (a, b) => a * b);
const n = nice(5);
const n2 = n.times(3).times(2); // nice.Number(30)
n()                             // 5
n2()                            // 30;
Check

Returns boolean. Never chenges it's arguments. After definition named Check can be used in 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.

nice.Action.Number.Number('times', (a, b) => a * b);
const n = nice(5);
n.times(3).times(2);            // n
n();                            // 30;

Interfaces

Switch

Delayed argumet

const f = nice.Switch
  .equal(1)(11)
  .number(22)
  .string.use(s => s + '!')
  .Nothing(':(')
  .default(42);
f(1);           //11
f(3);           //22
f('qwe');       //"qwe!"
f([]);          //42
f(0);           //42
f(undefined);   //:(
f(null);        //:(

Instant argument

nice.Check('meat', v => ['pork', 'beef'].includes(v));
const tiger = { say: console.log };
function feedTiger(tiger, food){
  tiger.hungry = nice.Switch(food)
    .meat(false)
    .default.use(name => tiger.say('I do not like ' + name) || true);
}

feedTiger(tiger, 'apple');   // tiger.hungry === true
// > I do not like apple

feedTiger(tiger, 'beef');    // tiger.hungry === false
Switch vs Function overload

Overloaded Function will search for best match while Switch will use first match.

nice.Function.Nothing(() => 1).Null(() => 2)(null);         //2
nice.Switch.Nothing.use(() => 1).Null.use(() => 2)(null);   //1

Besides current implementation of Switch use only first argument.

Boxes

Stateful observable components.

const { Box } = nice;
let b = Box(1);       // create box with 1 in it
b.listen(console.log) // listen for updates
b(2);                 // write value
b();                  // read value

//create Box that follows changes in b
let b2 = Box.use(b).by(n => n * 2);
b(3);                 // b2() === 6

//Named inputs
let square = Box()
  .Number('x', 5)
  .Number('y', 5)
  .by((x, y) => x * y);

square();                  // 25
square.x(10).y(b)();       // 30

Tag

const div = nice.Div('Normal ', 'text ')
  .I('italic ').up
  .add('normal ')
  .B('red bold').color('red').up
  .margin('10px')
  .fontSize('20px');

//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
div.show(); // attach dom node to document.body or provided node 

Add some Boxes to handle asynchronous cases.

const { Box, Div, Switch, Nothing } = nice;

const data = Box(Nothing);

const div = Box().use(data)
  .by(Switch
      .string.use(s => Div('Data: ', s))
      .default(Div('Loading...')));

div.listen(d => console.log(d.html));
// <div>Loading...</div>



data('Some data');
// <div>Data: Some data</div>


div.show(); // will create and attach dome node and update it's state according to boxes states

Keywords

FAQs

Package last updated on 17 Apr 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc