Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

helpers

Eser Ozvataf edited this page May 21, 2015 · 10 revisions

Each

Usage: $l.each(obj, function(key, value)[, testOwnProperties])

Alternative: $l.aeach for objects with 'length' property

$l.each(
    {a: 1, b: 2},
    function(i, item) { console.log(i + ' -> ' + item); }
);

prints

a -> 1
b -> 2

Map

Usage: $l.map(obj, function(value, key)[, dontSkipReturns, testOwnProperties])

Alternative: $l.amap for objects with 'length' property

$l.map(
    {a: 1, b: 2, c: 3},
    function(item, i) { if (item >= 2) { return i; } }
);

returns ["b", "c"]

Index

Usage: $l.index(obj, value[, testOwnProperties])

Alternative: $l.aindex for objects with 'length' property

$l.index(
    {a: 1, b: 2, c: 3},
    2
);

returns "b"

Extending objects

Usage: $l.extend(obj1, obj2)

// prints '{id: 1, name: 'eser', count: 5}'
console.log($l.extend({id: 1}, {name: 'eser', count: 5}));

Extending objects with namespaces

Usage: $l.extendNs(obj1, namespace, obj2)

// prints '{id: 1, name: 'eser', count: 5}'
$l.extendNs($l, 'plugins.test', {id: 1, name: 'eser', count: 5})
console.log($l.plugins.test);

Gets an unique id

Usage: $l.getUniqueId()

console.log($l.getUniqueId()); // prints 'uid-1'
console.log($l.getUniqueId()); // prints 'uid-2'

Generating query string

Usage: $l.buildQueryString(values[, rfc3986])

var element = $l('#form');
var serialized = $l.form.serialize(element);

// prints name=eser&surname=ozvataf&title=software%20designer
console.log($l.buildQueryString(serialized));

Generating FormData object

Usage: $l.buildFormData(values)

var element = $l('#form');
var serialized = $l.form.serialize(element);

serialized.remember = '1';
// prints FormData object
console.log($l.buildFormData(serialized));

Formatting a string

Usage: $l.format(format, arguments...)

var username = 'eser';
// prints 'name: eser'
console.log($l.format('name: %s', username));

Replacing all matches

Usage: $l.replaceAll(text, dictionary)

var dict = {'e': 'ii', 'a': 'o'};
// prints 'loroux.js hiilpiirs'
console.log($l.replaceAll('laroux.js helpers', dict));

Transform string into camel case

Usage: $l.camelCase(value)

// prints 'textAlign'
console.log($l.camelCase('text-align'));

Transform string back from camel case

Usage: $l.antiCamelCase(value)

// prints 'text-align'
console.log($l.antiCamelCase('textAlign'));

Encoding special characters

Usage: $l.quoteAttr(value)

// prints '<br clear="all" />'
console.log($l.quoteAttr('<br clear="all" />'));

Generating random value

Usage: $l.random(min, max)

// prints a random number between 1 and 5
console.log($l.random(1, 5));

Getting values from a single column

Usage: $l.column(obj, key)

var arr = [{id: 1, count: 5}, {id: 2, count: 12}];
// prints '[5, 12]'
console.log($l.column(arr, 'count'));

Shuffling array values

Usage: $l.shuffle(obj)

var arr = [1, 2, 3, 4, 5];
// prints '[3, 1, 2, 5, 4]'
console.log($l.shuffle(arr));

Duplicating an object

Usage: $l.duplicate(obj)

// prints '[5]'
console.log($l.duplicate([5]));

Getting count of elements

Usage: $l.getLength(obj)

// prints '3'
console.log($l.getLength({id: 1, name: 'eser', count: 5}));

Getting elements with dot notation

Usage: $l.getElement(obj, key[, defaultValue, delimiter])

// prints '1'
console.log($l.getElement({id: 1, child: { a: 1 }}, 'child.a'));

Getting keys for dot notation

Usage: $l.getKeysRecursive(obj[, delimiter, prefix, keys])

// prints '["id", "child", "child.a", "child.b"]'
console.log($l.getKeysRecursive({id: 1, child: { a: 1, b: 2 }}));