Created
September 4, 2019 14:23
-
-
Save wilcorrea/05f096b9d03f9ac4794673e857e3448d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /** | |
| * @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) | |
| } |
This file contains hidden or 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
| <template> | |
| <div> | |
| {{ $get(person, 'address.street') }} | |
| </div> | |
| </template> |
This file contains hidden or 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
| 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