@coop/utils

Misc utility functions

Stats

StarsIssuesVersionUpdatedCreatedSize
@coop/utils
001.1.53 years ago4 years agoMinified + gzip package size for @coop/utils in KB

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);

Source

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)

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.