1
0
mirror of synced 2024-11-29 08:36:12 +03:00
vue-formulario/src~v1/utils.js
2019-10-07 10:24:30 -04:00

96 lines
1.7 KiB
JavaScript

import cloneDeep from 'clone-deep'
/**
* Compare the equality of two arrays.
* @param {Array} arr1
* @param {Array} arr2
*/
export function equals (arr1, arr2) {
var length = arr1.length
if (length !== arr2.length) return false
for (var i = 0; i < length; i++) {
if (arr1[i] !== arr2[i]) {
return false
}
}
return true
}
/**
* Function to map over an object.
* @param {Object} obj An object to map over
* @param {Function} callback
*/
export function map (original, callback) {
let obj = cloneDeep(original)
for (let key in obj) {
obj[key] = callback(key, obj[key])
}
return obj
}
/**
* Function to filter an object's properties
* @param {Object} original
* @param {Function} callback
*/
export function filter (original, callback) {
let obj = {}
for (let key in original) {
if (callback(key, original[key])) {
obj[key] = original[key]
}
}
return obj
}
/**
* Function to reduce an object's properties
* @param {Object} original
* @param {Function} callback
* @param {*} accumulator
*/
export function reduce (original, callback, accumulator) {
for (let key in original) {
accumulator = callback(accumulator, key, original[key])
}
return accumulator
}
/**
* Comprehensive list of input types supported.
*/
export const inputTypes = {
text: [
'text',
'email',
'number',
'color',
'date',
'datetime-local',
'hidden',
'month',
'password',
'range',
'search',
'tel',
'time',
'url',
'week'
],
button: [
'submit',
'button'
],
select: [
'select'
],
box: [
'radio',
'checkbox'
],
textarea: [
'textarea'
]
}