Methods
# capitalize(str)
Capitalize provided string
Parameters:
Name | Type | Description |
---|---|---|
str |
string
|
The string to be capitalized |
- Version:
- v0.3.0
Example
capitalize("Lorem ipsum dolor sit amet") // "Lorem Ipsum Dolor Sit Amet"
# cartesian(…sets) → {Array.<Array.<T>>}
Cartesian product of multiple arrays
Credits: https://gist.github.com/ssippe/1f92625532eef28be6974f898efb23ef#gistcomment-3530882
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
sets |
Array.<Array.<T>>
|
<repeatable> |
The array(s) to be combine |
- Version:
- v0.1.0
Two-dimensional array of combination
Array.<Array.<T>>
Example
cartesian([1,2],[10,20],[100,200,300])
// [ [ 1, 10, 100 ],
// [ 1, 10, 200 ],
// [ 1, 10, 300 ],
// [ 1, 20, 200 ],
// [ 1, 20, 300 ],
// [ 2, 10, 100 ],
// [ 1, 20, 100 ],
// [ 2, 10, 200 ],
// [ 2, 10, 300 ],
// [ 2, 20, 100 ],
// [ 2, 20, 200 ],
// [ 2, 20, 300 ] ]
# clamp(n, min, max) → {number}
Clamps n within the min and max number
Parameters:
Name | Type | Description |
---|---|---|
n |
number
|
The number to clamp |
min |
number
|
The minimum number |
max |
number
|
The maximum number |
- Version:
- v0.1.0
Returns the clamped number
number
Example
clamp(-100, 0, 100) // 0
clamp(200, 0, 100) // 100
# debounce(func, wait, immediateopt)
https://www.educative.io/edpresso/how-to-use-the-debounce-function-in-javascript
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
func |
function
|
The function to invoke | |
wait |
number
|
The timeout in millisecond | |
immediate |
boolean
|
<optional> |
Invoke function immediately |
- Version:
- v0.1.0
# deepClone(value, hasFunctionOrUndefinedValuenullable) → {T}
Deep clone object or array
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
value |
T
|
||
hasFunctionOrUndefinedValue |
boolean
|
<nullable> |
- Version:
- v0.1.0
T
Example
const obj = { foo: { bar: 1 } }
const clone = deepClone(obj)
clone.foo.bar = 2;
console.log(obj.foo.bar) // 1
# deleteCookie(key, domainopt)
Delete Cookies value by key
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
key |
string
|
The Cookies key | |
domain |
boolean
|
<optional> |
To delete a Cookies that bound to the domain |
- Version:
- v0.1.0
# difference(left, right) → {Array.<T>}
Get the difference of two array of object
Parameters:
Name | Type | Description |
---|---|---|
left |
Array.<T>
|
The array to inspect |
right |
Array.<T>
|
The array to exclude |
- Version:
- v0.1.0
The excluded array of item
Array.<T>
Example
difference([1, 2], [2, 3])
// [1]
# differenceBy(left, right, key) → {Array.<T>}
Get the difference of two array
Parameters:
Name | Type | Description |
---|---|---|
left |
Array.<T>
|
The array to inspect |
right |
Array.<T>
|
The array to exclude |
key |
string
|
The key to inspect |
- Version:
- v0.1.0
The excluded array of item
Array.<T>
Example
differenceBy([{ x: 1 }, { x: 2 }], [{ x: 2 }, { x: 3 }], "x")
// [{ x: 1 }]
# formatMoney()
Format to a standard money string. Credits: https://stackoverflow.com/a/149099/6820538
- Version:
- v0.1.0
# get(obj, path, defaultValueopt) → {boolean}
Get value from provided path, if not found, return default value if any
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
obj |
Record.<string, any>
|
||
path |
string
|
||
defaultValue |
T
|
<optional> |
- Version:
- v0.1.0
boolean
Example
get({ foo: { bar: 1 } }, "foo.bar") // 1
get({ foo: { bar: 1 } }, "bar", null) // null
get({ foo: { bar: 1 } }, "bar") // undefined
# getAttribute(event, htmlAttribute) → (nullable) {string}
Get value by attribute from DOM
Parameters:
Name | Type | Description |
---|---|---|
event |
Event
|
|
htmlAttribute |
string
|
- Version:
- v0.1.0
string
Example
// Vue.js
<ul @click="handleClick">
<li data-id="1">1</li> // click target
<li data-id="2">2</li>
</ul>
<script>
export default {
methods: {
handleClick(e) {
const data = getAttribute(e, "data-id") // "1"
}
}
}
</script>
# getCookie(key) → {string}
Get Cookies value by key
Parameters:
Name | Type | Description |
---|---|---|
key |
string
|
The Cookies key |
- Version:
- v0.1.0
The Cookies value
string
# getElement(event, htmlAttribute) → (nullable) {HTMLElement}
Get DOM element by attribute from DOM
Parameters:
Name | Type | Description |
---|---|---|
event |
Event
|
|
htmlAttribute |
string
|
- Version:
- v0.1.0
HTMLElement
Example
// Vue.js
<ul @click="handleClick">
<li data-id="1">1</li> // click target
<li data-id="2">2</li>
</ul>
<script>
export default {
methods: {
handleClick(e) {
const data = getAttribute(e, "data-id") // <li>
}
}
}
</script>
# has(obj, path) → {boolean}
Check if key exist from provided path
Parameters:
Name | Type | Description |
---|---|---|
obj |
Record.<string, any>
|
|
path |
string
|
- Version:
- v0.1.0
boolean
Example
has({ foo: { bar: 1 } }, "foo.bar") // true
has({ foo: { bar: 1 } }, "bar") // false
# hyphenToPascalCase(str) → {string}
Convert hyphen case to pascal case
Parameters:
Name | Type | Description |
---|---|---|
str |
string
|
Hyphen case string |
- Version:
- v0.3.0
string
Example
hyphenToPascalCase("Lorem-ipsum-dolor-sit-amet") // "LoremIpsumDolorSitAmet"
# isEqual(val1, val2) → {boolean}
Check both value are equal
Parameters:
Name | Type | Description |
---|---|---|
val1 |
any
|
The value to inspect |
val2 |
any
|
The value to compare |
- Version:
- v0.1.0
boolean
Example
isEqual([1], [1]) // true
isEqual([1], [2]) // false
# isFalsey(val) → {boolean}
Check value is undefined, null, empty string, empty set,
empty map, empty array, empty object, negative value and of course false
Parameters:
Name | Type | Description |
---|---|---|
val |
any
|
The value to inspect |
- Version:
- v0.1.0
boolean
Example
isFalsey(0) // false
isFalsey(-1) // true
isFalsey(false) // true
# isObject(val) → {boolean}
Check value is object
Parameters:
Name | Type | Description |
---|---|---|
val |
any
|
The value to inspect |
- Version:
- v0.1.0
boolean
Example
isObject({}) // true
isObject([]) // false
# Modifier(name) → {generateBEM~string}
Generate Block Element Modifier string
Parameters:
Name | Type | Description |
---|---|---|
name |
string
|
Block name |
- Version:
- v0.3.0
generateBEM~string
Example
generateBEM("facil-button")() // "facil-button"
generateBEM("facil-button")("block") // "facil-button__block"
# move(arr, from, to) → {Array.<T>}
Move an element to provided index
Parameters:
Name | Type | Description |
---|---|---|
arr |
Array.<T>
|
The array to modify |
from |
number
|
The index of the element |
to |
number
|
The index of destination |
- Version:
- v0.1.0
Array.<T>
Example
move([1, 2, 3, 4], 0, 3)
// [2, 3, 4, 1]
# off(element, event, handler)
Shorthand to remove event listener
Parameters:
Name | Type | Description |
---|---|---|
element |
Element
|
HTMLElement
|
Document
|
Window
|
|
event |
DocumentEventMap
|
|
handler |
Func
|
- Version:
- v0.1.0
Example
function say() {
console.log("Hey mama!")
}
on(window, "click", say)
# on(element, event, handler)
Shorthand to add event listener
Parameters:
Name | Type | Description |
---|---|---|
element |
Element
|
HTMLElement
|
Document
|
Window
|
|
event |
DocumentEventMap
|
|
handler |
Func
|
- Version:
- v0.1.0
Example
function say() {
console.log("Hey mama!")
}
on(window, "click", say)
# random(min, max) → {number}
Randomize number between the min and max number
Parameters:
Name | Type | Description |
---|---|---|
min |
number
|
The minimum number |
max |
number
|
The maximum number |
- Version:
- v0.1.0
Returns the random number
number
# setCookie(key, value, durationopt)
Set Cookies
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
key |
string
|
The Cookies key | |
value |
string
|
The Cookies value | |
duration |
number
|
<optional> |
The Cookies duration |
- Version:
- v0.1.0
# slugify(str) → {string}
To slugify simple string, if you want to handle complex string,
you can check out https://www.npmjs.com/package/slugify.
Credits: https://stackoverflow.com/questions/1053902/how-to-convert-a-title-to-a-url-slug-in-jquery#comment34216351_1054862
Parameters:
Name | Type | Description |
---|---|---|
str |
string
|
The string to slugify |
- Version:
- v0.1.0
Returns slugified string
string
Example
slugify("THIS- IS AN URL") // "this-is-an-url"
# toObject(arr, keyopt) → {Record.<string, T>}
Convert an array to an object
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
arr |
Array.<T>
|
The array to convert | |
key |
string
|
<optional> |
The key as object's key |
- Version:
- v0.1.0
Record.<string, T>
Example
toObject([{ id: 1 , value: "foo" }, { id: 2, value: "bar" }], "id")
// {"1": { id: 1 , value: "foo" }, "2": { id: 2, value: "bar" }}
# truncate(str, lengthopt, ellipsisopt) → {string}
Truncate string if exceed specific length, pad end with ellipsis if any
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
str |
string
|
The string to truncate | |
length |
number
|
<optional> |
The maximum number of characters |
ellipsis |
string
|
<optional> |
The string pad at the end |
- Version:
- v0.1.0
Returns truncated string
string
Example
truncate("abcd", 3) // "abc"
truncate("abcd", 3, "...") // "abc..."
# uniqueArray(arr) → {Array.<T>}
Unique array's elements
Parameters:
Name | Type | Description |
---|---|---|
arr |
Array.<T>
|
The array to be uniquefy |
- Version:
- v0.1.0
The unique array
Array.<T>
Example
uniqueArray([1, 1, 2, 2])
// [1, 2]
# uniqueArrayByKey(arr, key) → {Array.<T>}
Unique array of object's elements by key
Parameters:
Name | Type | Description |
---|---|---|
arr |
Array.<T>
|
The array of object to be uniquefy |
key |
string
|
The key to inspect |
- Version:
- v0.1.0
The unique array of object
Array.<T>
Example
uniqueArray([{ x: 1, y: 1 }, { x: 2, y: 1 }, { x: 2, y: 2 }])
// [{ x: 1, y: 1}, { x: 2, y: 2 }]