-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d432e8b
commit a897736
Showing
133 changed files
with
1,271 additions
and
225 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
'use strict' | ||
import { unique } from './utils/index.mjs' | ||
import { get } from './utils/object.mjs' | ||
import { slugify } from './utils/string.mjs' | ||
|
||
/** | ||
* Tests if is whole number. returns false if n is Float | ||
* @param {String|Number} n | ||
*/ | ||
export const isInt = n => Number.isInteger(Number(n)) | ||
|
||
/** | ||
* Finds the index of an element in its parent | ||
* @param {NodeElement} node | ||
* @param {NodeElement} parent | ||
*/ | ||
export const indexOfNode = (node, parent) => { | ||
const parentElement = parent || node.parentElement | ||
const nodeList = Array.prototype.slice.call(parentElement.childNodes) | ||
return nodeList.indexOf(node) | ||
} | ||
|
||
/** | ||
* Orders an array of objects by specific attributes | ||
* @param {Array} elements Array of element objects | ||
* @param {Array} order array of keys to order objects by | ||
* @param {String} path string path to property to order by | ||
* @return {Array} Ordered Array of Element Objects | ||
*/ | ||
export const orderObjectsBy = (elements, order, path) => { | ||
// OR operator for path | ||
const splitPath = path.split('||') | ||
const newOrder = unique(order) | ||
.map(key => | ||
elements.find(elem => { | ||
const newPath = splitPath.find(p => !!get(elem, p)) | ||
return newPath && get(elem, newPath) === key | ||
}), | ||
) | ||
.filter(Boolean) | ||
const orderedElements = newOrder.concat(elements) | ||
|
||
return unique(orderedElements) | ||
} | ||
|
||
/** | ||
* @param {Array|NodeList} arr to be iterated | ||
* @param {Function} cb | ||
* @param {Context} | ||
*/ | ||
export const forEach = (arr, cb, scope) => { | ||
for (let i = 0; i < arr.length; i++) { | ||
cb.call(scope, arr[i], i) | ||
} | ||
} | ||
|
||
/** | ||
* @param {Array|NodeList} arr to be iterated | ||
* @param {Function} cb | ||
* @return {Array} newArray | ||
*/ | ||
export const map = (arr, cb) => { | ||
const newArray = [] | ||
forEach(arr, (elem, i) => newArray.push(cb(elem, i))) | ||
|
||
return newArray | ||
} | ||
|
||
export const safeAttrName = name => { | ||
const safeAttr = { | ||
className: 'class', | ||
} | ||
|
||
return safeAttr[name] || slugify(name) | ||
} | ||
|
||
export const capitalize = str => str.replace(/\b\w/g, m => m.toUpperCase()) | ||
|
||
// Expensive recursive object copy | ||
export const copyObj = obj => window.JSON.parse(window.JSON.stringify(obj)) | ||
|
||
// subtract the contents of 1 array from another | ||
export const subtract = (arr, from) => from.filter(a => !~arr.indexOf(a)) | ||
|
||
export const isIE = () => window.navigator.userAgent.indexOf('MSIE ') !== -1 | ||
|
||
export const helpers = { | ||
capitalize, | ||
safeAttrName, | ||
forEach, | ||
copyObj, | ||
|
||
// basic map that can be used on nodeList | ||
map, | ||
subtract, | ||
indexOfNode, | ||
isInt, | ||
get, | ||
|
||
orderObjectsBy, | ||
isIE, | ||
} | ||
|
||
export default helpers |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.