Lightweight common vanilla utilities for the modern web development
Table of Contents
- Install
- Usage
- Array Commons
- Date Commons
- Element Commons
- Function Commons
- Number Commons
- Object Commons
- String Commons
- Prior Art
- Contributing
- License
Install
This project uses node and npm. Go check them out if you don't have them locally installed.
$ npm install --save vanilla-commons
The UMD build is also available on jsdelivr:
You can find the library on window.commons
.
Usage
const date = 'December 17, 1995 03:24:00'const format = '{DD}/{MM}/{YYYY} {HH}:{mm}:{ss}:{ms}' const finalDate = date// '24/03/1993 12:25:00:00'
Array Commons
flattenArray(arr)
Flatten nested arrays.
Parameters
arr
Array Nested array.
Returns
Array flattened.
Examples
// [1, 2, 3, 4]
Date Commons
addDays(days, date)
Add days to a Date.
Parameters
Returns
Date with the days added.
Examples
// Sat, 23 Dec 1995 03:24:00 // Mon, 11 Dec 1995 03:24:00 'December 17, 1995 03:24:00'// Sat, 23 Dec 1995 03:24:00
addHours(hours, date)
Add hours to a Date.
Parameters
Returns
Date with the hours added.
Examples
// Sun, 17 Dec 1995 09:24:00 // Fri, 16 Dec 1995 21:24:00 'December 17, 1995 03:24:00'// Sun, 17 Dec 1995 09:24:00
addMilliseconds(milliseconds, date)
Add milliseconds to a Date.
Parameters
Returns
Date with the milliseconds added.
Examples
// Sun, 17 Dec 1995 09:24:01 // Sun, 17 Dec 1995 09:23:59 'December 17, 1995 03:24:00'// Sun, 17 Dec 1995 09:24:01
addMinutes(minutes, date)
Add minutes to a Date.
Parameters
Returns
Date with the minutes added.
Examples
// Sun, 17 Dec 1995 09:24:06 // Sun, 17 Dec 1995 09:23:54 'December 17, 1995 03:24:00'// Sun, 17 Dec 1995 09:24:06
addMonths(months, date)
Add months to a Date.
Parameters
Returns
Date with the months added.
Examples
// Mon, 17 Jun 1996 09:24:00 // Sat, 17 Jun 1995 09:24:00 'December 17, 1995 03:24:00'// Mon, 17 Jun 1996 09:24:00
addSeconds(seconds, date)
Add seconds to a Date.
Parameters
Returns
Date with the seconds added.
Examples
// Sun, 17 Dec 1995 09:30:00 // Sun, 17 Dec 1995 09:18:00 'December 17, 1995 03:24:00'// Sun, 17 Dec 1995 09:30:00
addWeeks(weeks, date)
Add weeks to a Date.
Parameters
Returns
Date with the weeks added.
Examples
// Sun, 24 Dec 1995 09:24:00 // Sun, 10 Dec 1995 09:24:00 'December 17, 1995 03:24:00'// Sun, 24 Dec 1995 09:24:00
addYears(years, date)
Add years to a Date.
Parameters
Returns
Date with the years added.
Examples
// Tue, 17 Dec 1996 09:24:00 // Sat, 17 Dec 1994 09:24:00 'December 17, 1995 03:24:00'// Tue, 17 Dec 1996 09:24:00
diffDate(firstDate, secondDate)
Calculate the diff of two Date objects.
Parameters
Returns
Date with the years added.
Examples
// {// milliseconds: 31622460000,// seconds: 31622460,// minutes: 527041,// hours: 8784.02,// days: 366,// weeks: 52.29,// months: 12.2,// years: 1// }
formatDate(format, date)
Format a date given a expected format. Use the following patterns inside your format:
{YYYY}
: full year; 2017{YY}
: short year; 17{MM}
: month; 04{DD}
: day; 01{HH}
: hours; 06 (24h){mm}
: minutes; 59{ss}
: seconds; 09{ms}
: milliseconds; 10
Parameters
Returns
string formatted date.
Examples
// '17/12/1995 03:24:00:00'
isValidDate(format, dateStr)
Checks the validity of a given date string. Use the following patterns inside your format:
{YYYY}
: full year; 2017{YY}
: short year; 17{MM}
: month; 04{DD}
: day; 01{HH}
: hours; 06 (24h){mm}
: minutes; 59{ss}
: seconds; 09{ms}
: milliseconds; 10
Parameters
Returns
Boolean true if the date is valid.
Examples
// true // false
parseDate(format, dateStr)
Parse a date string to a date object given a format. Use the following patterns inside your format:
{YYYY}
: full year; 2017{YY}
: short year; 17{MM}
: month; 04{DD}
: day; 01{HH}
: hours; 06 (24h){mm}
: minutes; 59{ss}
: seconds; 09{ms}
: milliseconds; 10
Parameters
Returns
Date parsed date.
Examples
// Sun, 17 Dec 1995 03:24:00
Element Commons
addClass(newClass, element)
Add a class to a DOM Element.
Parameters
newClass
(Array | string) Array or string of class to add to a DOM Element.element
Element Element to apply the changes.
Returns
Element in which the changes were made.
Examples
const element = documentelement// <div class="element hey"></div> // <div class="element hey there man"></div>
clearEvents(element)
Remove all the event listeners of a element and its children.
Parameters
element
Element Element that will have its events removed.
Returns
Element element that the events were removed.
Examples
// See the tests for better examples https://github.com/gugutz/vanilla-commons/blob/master/test/element/clear-events.test.js const element = document
createElement(selector?, props?, children?)
Create a DOM element.
Parameters
selector
string CSS selector like.props
Object Properties of the node.children
(string|Element|Array) Children of the element.
Returns
Element created.
Examples
// See the tests for better examples https://github.com/gugutz/vanilla-commons/blob/master/test/element/create-element.test.js // <div></div> // <input id="id" class="class" type="text" disabled required/> // <div data-ref="hey" data-error="bad"></div> // <p class="hey there man"></p> // It's easy to work with events! // <div>// <input type=""/>// hey// <img src="coolimage.jpg"/>// </div>
getParents(element)
Get all the parents of a given element.
Parameters
element
Element Reference element.
Returns
Array the parents of the element, including the body.
Examples
// See the tests for better examples https://github.com/gugutz/vanilla-commons/blob/master/test/element/get-parents.test.js const element = document
hasClass(classToCheck, element)
Check if a DOM Element has the specified class.
Parameters
classToCheck
(Array | string) Array or string of class to check the existence in a DOM Element.element
Element Element to apply the changes.
Returns
Element to check if has the specified class.
Examples
const element = documentelementclassName = 'hey there' element// true // true // false // true // false
hideElement(elementToHide)
Hide a Element from the DOM.
Parameters
Returns
(Array | Element) element or array of elements that were hidden.
Examples
const element = documentelementstyledisplay// block elementstyledisplay// none
const element1 = documentelement1styledisplay// block const element2 = documentelement2styledisplay// block element1styledisplay// none element2styledisplay// none
insertElementAfter(referenceElement, newElement)
Insert a element after a reference element.
Parameters
Returns
Element that were inserted.
Examples
insertElementBefore(referenceElement, newElement)
Insert a element before a reference element.
Parameters
Returns
Element that were inserted.
Examples
killElement(elementToKill)
Kill a Element from the DOM.
Parameters
Examples
removeClass(classToRemove, element)
Remove a class of a DOM Element.
Parameters
classToRemove
(Array | string) Array or string of class to remove of a DOM Element.element
Element Element to apply the changes.
Returns
Element that the had the class removed.
Examples
element
replaceElement(originalElement, newElement)
Replace a DOM element with another element.
Parameters
originalElement
Element Element to be replaced.newElement
Element New element to replace the old one.
Returns
Element element that replaced the old one.
Examples
showElement(elementToShow)
Show a hidden Element from the DOM.
Parameters
Returns
(Array | Element) element or array of elements that were showed.
Examples
toggleClass(classToToggle, element)
Toggle a class from a DOM Element.
Parameters
classToToggle
(Array | string) Array or string of class to toggle a DOM Element.element
Element to apply the changes.
Returns
Element that the changes were made.
Examples
element
Function Commons
compose(fn[, fn1, ..., fnN])
Performs right-to-left function composition.
Parameters
fn
...fnN
Function functions to be composed.
Returns
- Function composed function.
Examples
const add = const multiply = const composedFunction = // (((2 + 3) * 4) + 5) = 25
curry(fn)
Returns a curried equivalent of the provided function
Parameters
fn
Function function to be curried.
Returns
- Function curried function.
Examples
const addFourNumbers = a + b + c + d const curriedAddFourNumbers = 34// 10 3 4// 10 234// 10 4// 10
pipe(fn[, fn1, ..., fnN])
Performs left-to-right function composition.
Parameters
fn
...fnN
Function functions to be piped.
Returns
- Function piped function.
Examples
const add = const multiply = const pipedFunction = // (((2 + 5) * 4) + 3) = 31
throttle(limit, fn)
Creates a function that will call fn at most once every wait milliseconds.
Parameters
Returns
- Function throttled function.
Examples
window
Number Commons
round(num)
Round a number to two decimal places.
Parameters
num
Number number to be rounded.
Returns
Number rounded number.
Examples
// 3.14 // -27.82
Object Commons
mapKeys(fn, obj)
Map over the keys of a object.
Parameters
Returns
Object mapped object.
Examples
// {// captainhey: 'picard',// firstOfficerhey: 'riker'// }
mapValues(fn, obj)
Map over the values of a object.
Parameters
Returns
Object mapped object.
Examples
// {// captain: 'picardhey',// firstOfficer: 'rikerhey'// }
String Commons
capitalize(str)
Converts the first character of string to upper case.
Parameters
str
string string to be capitalized.
Returns
string capitalized string.
Examples
// Vanilla
cleanUpString(str)
Removes the all line breaks in the entire string and also the white spaces from the both ends of the string.
Parameters
str
string string to be cleaned.
Returns
string clean string.
Examples
// '' // 'hjhj'
Prior Art
Vanilla Commons is fully inspired by these projects:
- Lodash
- Underscore.js
- Pareto.js
- Ramda
- tinydate
- date-fns
- Moment.js
- jQuery
- dom-create-element-query-selector
- hyperscript
Contributing
See the contributing file.