API Docs for: 1.2
Show:

hoon Class

Defined in: src/hoon.js:4

Methods

applyConstructor

(
  • Constructor
  • args
)
Constructor

Defined in src/hoon.js:266

create an instance of Constructor with a given arguments provided as an array.

Parameters:

  • Constructor Function

    A constructor function.

  • args Array

Returns:

Constructor:

An instance of Constructor.

Example:

function Foo(name, age){
    this.name = name;
    this.age = age;
}
var foo = hoon.applyConstructor(Foo, ["John", 20]); // new Foo("John", 20);

clone

(
  • obj
)
Object

Defined in src/hoon.js:210

Return the pseudo deep clone of the argument.

Parameters:

  • obj Object

    obj must be comprise of the following objects.

    • literal number(ex: 1)
    • literal string(ex: "hello")
    • undefined, null, NaN
    • true, false
    • Boolean, Number, String, RegExp, Date, Array, Object

Returns:

Object:

The deep clone of the argument.

Example:

// ex1
var obj = {0:{1:1}};
var ret = hoon.clone(obj);
console.log(obj); // {0:{1:1}};
console.log(ret); // {0:{1:1}};
ret[0][1] = 3;
console.log(obj); // {0:{1:1}};
console.log(ret); // {0:{1:3}};

extend

(
  • *sources
)
Object

Defined in src/hoon.js:146

Union sources's properties and return the new object. Unlike underscore's extend, the first argument is not changed.

Parameters:

  • *sources Object

    This method is passed the arbitary number of objects as arguments. It's in-order, so the last source will override properties of the same name in previous arguments.

Returns:

Object:

Example:

// ex1
var s1 = {0:0, 1:1, 2:2};
var s2 = {1:3, 2:5, 3:3, 4:4};
var s3 = {2:7, 5:8};
var ret = hoon.extend(s1, s2, s3);
console.log(ret); // {0:0, 1:3, 2:7, 3:3, 4:4, 5:8}
console.log(s1); // {0:0, 1:1, 2:2}
console.log(s2); // {1:3, 2:5, 3:3, 4:4}
console.log(s3); // {2:7, 5:8}
// ex2
console.log(hoon.extend()); // {}

extract

(
  • obj
  • extract
)
Object

Defined in src/hoon.js:12

Extract object's properties and return the new object.

Parameters:

  • obj Object
  • extract Function

Returns:

Object:

Example:

// ex1
var obj = { 0:0, 1:1, 2:2, 3:3 };
var ret = hoon.extract(obj, function(val, key){
    return (key % 2) == 0;
});
console.log(ret); // { 0:0, 2:2 }
console.log(obj); // { 0:0, 1:1, 2:2, 3:3 };
// ex2
var obj = { 0:0, 1:1, 2:2, 3:3 };
var ret = hoon.extract(obj, function(val){
    return val < 2;
});
console.log(ret); // { 0:0, 1:1 }
console.log(obj); // { 0:0, 1:1, 2:2, 3:3 };
// ex3
var obj = { 0:0, 1:1, 2:2, 3:3 };
var ret = hoon.extract(obj, function(val){
    return val > 3;
});
console.log(ret); // {}
console.log(obj); // { 0:0, 1:1, 2:2, 3:3 };

fmap

(
  • obj
  • fmap
)
Object

Defined in src/hoon.js:57

Extract object's properties, convert and return the new object.

fmap = filter + map

Parameters:

  • obj Object
  • fmap Function

Returns:

Object:

Example:

// ex1
var obj = { 0:0, 1:1, 2:2, 3:3 };
var ret = hoon.fmap(obj, function(val, key){
    if ((key % 2) === 1){
        return [key, key * val];
    }
});
console.log(ret); // { 1:1, 3:9 }
console.log(obj); // { 0:0, 1:1, 2:2, 3:3 };
// ex2
var obj = { 0:0, 1:1, 2:2, 3:3 };
var ret = hoon.fmap(obj, function(val){
    if (val > 3){
        return [key, val];
    }
});
console.log(ret); // {}
console.log(obj); // { 0:0, 1:1, 2:2, 3:3 };
// ex3
var obj = {};
var ret = hoon.fmap(obj, function(val, key){
    if (val > 3){
        return [key, val];
    }
});
console.log(ret); // {}
console.log(obj); // {}

makeObject

(
  • keys
  • values
)
Object

Defined in src/hoon.js:292

create an objects.

Parameters:

  • keys String | Array of String
  • values Anything

Returns:

Object:

Example:

var keys = "name";
var values = "John";
console.log(hoon.makeObject(keys, values)); // { name: "John" }
keys = ["name", "age"];
values = ["John", 20];
console.log(hoon.makeObject(keys, values)); // { name: "John", age: 20 }
var values = "John";
console.log(hoon.makeObject(keys, values)); // { name: "John", age: "John" }
var values = [["John", 20]];
console.log(hoon.makeObject(keys, values)); // { name: ["John", 20], age: ["John", 20] }

padding

(
  • str
  • len
  • char
  • [flag=false]
)
String

Defined in src/hoon.js:110

Pad a string with given characters.

Parameters:

  • str String

    A padded string.

  • len Integer

    The max length of the return value.

  • char String
  • [flag=false] Boolean optional

    If flag is true, str is padded to the rigtht, else left.

Returns:

String:

Example:

// ex1
var ret = hoon.padding("5", 3, "0");
console.log(ret); // "005"
// ex2
var ret = hoon.padding("hello", 13, "<*>", true);
console.log(ret); // "hello<*><*>"

templates

(
  • obj
)
Object

Defined in src/hoon.js:183

The helper method of underscore's template.

Parameters:

  • obj Object

Returns:

Object:

Example:

// ex1
var ret = hoon.templates({
    hello: "Hello, <%= name %>.",
    date, "<%= month %>/<%= day %>",
});
console.log(ret.hello({name: "John"})); // Hello, John.
console.log(ret.date({month: 10, day: 12})); // 10/12
// ex2
var ret = hoon.templates({}); // {}