UNPKG

@hackplan/polaris

Version:

Shopify’s product component library

24 lines (23 loc) 652 B
const OBJECT_NOTATION_MATCHER = /\[(.*?)\]|(\w+)/g; export function get(obj, keypath, defaultValue) { if (obj == null) return undefined; const keys = Array.isArray(keypath) ? keypath : getKeypath(keypath); let acc = obj; for (let i = 0; i < keys.length; i++) { const val = acc[keys[i]]; if (val === undefined) return defaultValue; acc = val; } return acc; } function getKeypath(str) { const path = []; let result; while ((result = OBJECT_NOTATION_MATCHER.exec(str))) { const [, first, second] = result; path.push(first || second); } return path; }