ans-ui
Version:
A vue-based UI components library for analysys
51 lines (46 loc) • 1.05 kB
JavaScript
/**
* A simple uuid generator, support prefix and template pattern.
*
* @example
*
* uuid('v-') // -> v-xxx
* uuid('v-ani-%{s}-translate') // -> v-ani-xxx
*/
function uuid (prefix) {
var id = Math.floor(Math.random() * 10000).toString(36)
return prefix ? (
~prefix.indexOf('%{s}') ? (
prefix.replace(/%\{s\}/g, id)
) : (prefix + id)
) : id
}
const getValueByPath = (model, path) => {
if (!path || !model) return ''
if (path.indexOf('.') < 0) {
return getArrayValue(model, path)
}
const key = path.split('.')
let current = model
for (let i = 0; i < key.length; i++) {
const currentKey = key[i]
current = getArrayValue(current, currentKey)
}
return current
}
function getArrayValue (model, key) {
if (key.includes('[')) {
const parts = key.split(/[[\]]/).filter(p => p !== '')
let value = model[parts[0]]
for (const index of parts.slice(1)) {
value = value[index]
}
return value
} else {
return model[key]
}
}
export {
uuid,
getValueByPath
}