Giter VIP home page Giter VIP logo

ducky's Introduction

Ducky โ€” duckyjs.com

Duck-Typed Value Handling for JavaScript

Abstract

Ducky is a small Open-Source JavaScript library, providing Duck-Typed Value Validation, Value Selection and Flexible Function Parameter Handling. It can be used in Node.js based server and browser based client environments.

Getting Ducky

You can conveniently get Ducky in various ways:

  • Bower: install as client component via the Bower component manager:
    $ bower install ducky

  • NPM: install as server component via the Node Package Manager:
    $ npm install ducky

  • Git: directly clone the official repository:
    $ git clone https://github.com/rse/ducky.git

  • cURL: download only the main file from the repository:
    $ curl -O https://raw.github.com/rse/ducky/master/lib/ducky.js

API

Ducky provides the following API:

ducky.version = { major: Number, minor: Number, micro: Number, date: Number }

The version of Ducky, provided as a tuple of separate pieces, for easy comparison.

if (!(ducky.version.major >= 2 && ducky.version.minor >= 0))
    throw new Error("need at least Ducky 2.0.0");

ducky.select(object: Object, path: String, value?: Object): Object

Dereference into (and this way subset) object according to the path specification and either return the dereferenced value or set a new value. Object has to be a hash or array object. The path argument has to follow the following grammar (which is a direct JavaScript dereferencing syntax):

LHS RHS
path ::= segment segment*
segment ::= bybareword | bykey
bybareword ::= "."? identifier
bykey ::= "[" key "]"
identifier ::= /[_a-zA-Z$][_a-zA-Z$0-9]*>/
key ::= number | squote | dquote
number ::= /[0-9]+/
dquote ::= `/"(?:\"
squote ::= `/'(?:\'

Setting the value to undefined effectively removes the dereferenced value. If the dereferenced parent object is a hash, this means the value is delete'ed from it. If the dereferenced parent object is an array, this means the value is splice'ed out of it.

ducky.select({ foo: { bar: { baz: [ 42, 7, "Quux" ] } } },
    "foo['bar'].baz[2]") // → "Quux"

In case caching of the internally compiled Abstract Syntax Tree (AST) is not wishes, you can perform the compile and execute steps of ducky.select individually:

ducky.select.compile(path: String): Object

Compile the selection specification path into an AST.

ducky.select.execute(object: Object, ast: Object, value?: Object): Object

Select from object a value via ast and either return it or set it to the new value value.

validate(object: Object, spec: String): Boolean

Validate an arbitrary nested JavaScript object object against the specification spec. The specification spec has to be either a RegExp object for String validation, a validation function of signature spec(Object): Boolean or a string following the following grammar (which is a mixture of JSON-like structure and RegExp-like quantifiers):

LHS RHS
spec ::= not | alt | hash | array | any | primary | class
not ::= "!" spec
alt ::= "(" spec ("|" spec)* ")"
hash ::= "{" (key arity? ":" spec ("," key arity? ":" spec)*)? "}"
array ::= "[" (spec arity? ("," spec arity?)*)? "]"
arity ::= "?" | "*" | "+" | "{" number "," (number | "oo") "}"
number ::= /^[0-9]+$/
key ::= /^[_a-zA-Z$][_a-zA-Z$0-9]*$/ | "@"
any ::= "any"
primary ::= `/^(?:null
class ::= /^[A-Z][_a-zA-Z$0-9]\*$/

The special key @ can be used to match an arbitrary hash element key.

ducky.validate({ foo: "Foo", bar: "Bar", baz: [ 42, 7, "Quux" ] },
    "{ foo: string, bar: any, baz: [ number+, string* ], quux?: any }") // &arr; true

In case caching of the internally compiled Abstract Syntax Tree (AST) is not wishes, you can perform the compile and execute steps of ducky.validate individually:

ducky.validate.compile(spec: String): Object

Compile the validation specification spec into an AST.

ducky.validate.execute(object: Object, ast: Object): Boolean

Validate object against ast and return true in case it validates.

ducky.params(name: String, args: Object[], spec: Object): Object

Handle positional and named function parameters by processing a function's arguments array. Parameter name is the name of the function for use in exceptions in case of invalid parameters. Parameter args usually is the JavaScript arguments pseudo-array of a function. Parameter spec is the parameter specification: each key is the name of a parameter and the value has to be an Object with the following possible fields: pos for the optional position in case of positional usage, def for the default value (of not required and hence optional parameters), req to indicate whether the parameter is required and valid for type validation (either a string accepted by the validate>() method, or a valid regular expression /.../ object for validating a String against it or an arbitrary validation callback function of signature valid(Object): Boolean.

function config () {
    var params = ducky.params("config", arguments, {
        scope: { pos: 0, req: true,      valid: "boolean"           },
        key:   { pos: 1, req: true,      valid: /^[a-z][a-z0-9_]*$/ },
        value: { pos: 2, def: undefined, valid: "object"            },
        force: {         def: false,     valid: "boolean"           }
    });
    var result = cfg_get(params.scope, params.key);
    if (typeof params.value !== "undefined")
        cfg_set(params.scope, params.key, params.value, params.force);
    return result;
}
var value = config("foo", "bar");
config("foo", "bar", "quux");
config({ scope: "foo", key: "bar", value: "quux", force: true });

License

Copyright (c) 2010-2013 Ralf S. Engelschall (http://engelschall.com/)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

ducky's People

Contributors

rse avatar

Watchers

 avatar  avatar

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.