Giter Site home page Giter Site logo

gamtiq / eva Goto Github PK

View Code? Open in Web Editor NEW
15.0 5.0 1.0 704 KB

Functions to create and call functions and evaluate expressions

License: MIT License

JavaScript 98.35% CSS 1.49% HTML 0.17%
eval evaluate expression execute delegate closure function method dynamic

eva's Introduction

eva

Functions to create and call functions and evaluate expressions.

NPM version Build Status Built with Grunt

Installation

Node

npm install eva
bower install eva

AMD, <script>

Use dist/eva.js or dist/eva.min.js (minified version).

Usage

Node

var eva = require("eva");

AMD

define(["path/to/dist/eva.js"], function(eva) {
    ...
});

Bower, <script>

<!-- Use bower_components/eva/dist/eva.js if the library was installed by Bower -->

<script type="text/javascript" src="path/to/dist/eva.js"></script>
<script type="text/javascript">
    // eva is available via eva field of window object
    ...
</script>

Examples

var func = eva.createFunction("(a || 0) + (b || 0) + (c || 0)", {paramNames: "a, b, c", expression: true});
console.log(func("abc"));   // abc00
console.log(func(10, 1, 5, 8));   // 16

func = eva.createFunction("if (obj.b) {return a + b;} else {return 'a=' + a;}", {scope: true, paramNames: "obj"});
console.log( func({a: "a", b: "bc"}) );   // abc

console.log( eva.evalWith("this.a + this.b", {a: 1, b: 9}) );   // 10
console.log( eva.evalWith("fn(this.expr)", {expr: "Math.sin(0)"}, {fn: eva.evalWith}) );   // 0

var obj = {};
eva.createDelegateMethod(eva, "evalWith", {destination: obj, destinationMethod: "expr"});
console.log( obj.expr("Math.cos(0)") );   // 1

func = eva.closure(eva.evalWith, ["this.a * this.b"]);
console.log( func({a: 4, b: 7}) );   // 28
console.log( func({a: -3, b: 5}) );   // -15

func = eva.closure(eva.evalWith, [{a: 3, b: -9}], null, {prependArgs: true});
console.log( func("this.a + this.b") );   // -6
console.log( func("this.a - this.b") );   // 12

var funcList = [
    eva.closure(eva.evalWith, ["this.a + this.b"]),
    eva.closure(eva.evalWith, ["this.a * this.b"]),
    eva.closure(eva.evalWith, ["Math.max(this.a, this.b)"])
];
console.log( eva.map(funcList, [{a: 2, b: -7}]) );   // [-5, -14, 2]

API

createFunction(code: String, [settings: Object]): Function

Create function to further use.

evalWith(expression: String, [context: Object], [scope: Object])

Calculate/evaluate value of specified expression using given context and scope.

createDelegateMethod(delegate: Object, method: String, [settings: Object]): Function

Create function that executes specified method of the given object.

closure(action: Function, [paramList: Array], [context: Object], [settings: Object]): Function

Create function that executes specified function with given parameters and context and returns result of call.

map(funcList: Array, [paramList: Array | Function], [context: Object | Function], [settings: Object]): Array

Call each function from specified list and return array containing results of calls.

See doc folder for details.

Related projects

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

License

Copyright (c) 2014-2018 Denis Sikuler
Licensed under the MIT license.

eva's People

Contributors

gamtiq avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

zafisoft

eva's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.