@coop/utils
v1.1.5
Published
Misc utility functions
Downloads
12
Readme
DEPRECATED - Coop Utils
Misc utility functions to use in Coop projects
Part of Coop Trolley
Installation
npm install @coop/utils --save
import utils from '@coop/utils'
Sort Alphabetically
Sort arrays alphabetically.
Usage
utils.sortAlphabetically(array, key, asc = true)
Example
const arr = [ { name: 'foo' }, { name: 'bar' } ]
const result = utils.sortAlphabetically(arr, 'name')
console.log(result)
// [ { name: 'bar' }, { name: 'foo' } ]
Emit Event
Cross browser compliant eventEmitter.
Usage
utils.emitEvent(eventName, value = null)
Example
utils.emitEvent('trigger-something')
Debounce
Debounce script to avoid firing tasks to often
Example
const fn = utils.debounce(() => {
// Do stuff
}, 200);
window.addEventListener('resize', fn);
Dom Helpers
Misc dom helpers to use instead of jQuery
Usage
// Get parsed JSON data stored as data attribute on element
getParsedDomDataFromId(id, dataAttribute)
// Get unparsed data from attribute on element
getDomDataFromId(id, dataAttribute)
// Get each of element
forEachSelector('.class-name', element => {
// Do stuff with element
})
// Fires when dom is ready
domReady(myFunc)
// Remove class from element
removeClass(targetElement, classToRemove, optionalIndex)
// Returns parsed data from multiple data attributes of element. Will parse JSON if formatted.
getAllDataFromElement(targetElement)