Skip to content

Instantly share code, notes, and snippets.

@wilcorrea
Created September 4, 2019 14:23
Show Gist options
  • Save wilcorrea/05f096b9d03f9ac4794673e857e3448d to your computer and use it in GitHub Desktop.
Save wilcorrea/05f096b9d03f9ac4794673e857e3448d to your computer and use it in GitHub Desktop.
/**
* @param {Object} element
* @param {String|Array} path
* @param {*} fallback
* @returns {*}
*/
export const get = (element: any, path: any, fallback?: any): any => {
if (element === undefined || element === null) {
return fallback
}
const search = Array.isArray(path)
? path
: path.split('.').filter((pieces: any) => pieces && pieces.length)
if (!search.length) {
return element
}
let property = search.shift()
if (Array.isArray(element)) {
// eslint-disable-next-line no-useless-escape
property = String(property).replace(/[\[\]]+/g, '')
}
return get(element[property], search, fallback)
}
<template>
<div>
{{ $get(person, 'address.street') }}
</div>
</template>
import { get } from 'general'
/**
* @param Vue
*/
export default ({ Vue }) => {
/**
*/
Object.defineProperty(Vue.prototype, '$get', {
get () {
return get
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment