hoon Class
Item Index
Methods
applyConstructor
-
Constructor
-
args
create an instance of Constructor with a given arguments provided as an array.
Parameters:
-
Constructor
FunctionA constructor function.
-
args
Array
Returns:
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
Return the pseudo deep clone of the argument.
Parameters:
-
obj
Objectobj 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:
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
Union sources's properties and return the new object. Unlike underscore's extend, the first argument is not changed.
Parameters:
-
*sources
ObjectThis 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:
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
Extract object's properties and return the new object.
Parameters:
-
obj
Object -
extract
Function
Returns:
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
Extract object's properties, convert and return the new object.
fmap = filter + map
Parameters:
-
obj
Object -
fmap
Function
Returns:
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
create an objects.
Parameters:
-
keys
String | Array of String -
values
Anything
Returns:
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]
Pad a string with given characters.
Parameters:
-
str
StringA padded string.
-
len
IntegerThe max length of the return value.
-
char
String -
[flag=false]
Boolean optionalIf flag is true, str is padded to the rigtht, else left.
Returns:
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
The helper method of underscore's template.
Parameters:
-
obj
Object
Returns:
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({}); // {}