UNPKG

1.23 kBJavaScriptView Raw
1/* @flow */
2
3/**
4 * unicode letters used for parsing html tags, component names and property paths.
5 * using https://www.w3.org/TR/html53/semantics-scripting.html#potentialcustomelementname
6 * skipping \u10000-\uEFFFF due to it freezing up PhantomJS
7 */
8export const unicodeLetters = 'a-zA-Z\u00B7\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u037D\u037F-\u1FFF\u200C-\u200D\u203F-\u2040\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD'
9
10/**
11 * Check if a string starts with $ or _
12 */
13export function isReserved (str: string): boolean {
14 const c = (str + '').charCodeAt(0)
15 return c === 0x24 || c === 0x5F
16}
17
18/**
19 * Define a property.
20 */
21export function def (obj: Object, key: string, val: any, enumerable?: boolean) {
22 Object.defineProperty(obj, key, {
23 value: val,
24 enumerable: !!enumerable,
25 writable: true,
26 configurable: true
27 })
28}
29
30/**
31 * Parse simple path.
32 */
33const bailRE = new RegExp(`[^${unicodeLetters}.$_\\d]`)
34export function parsePath (path: string): any {
35 if (bailRE.test(path)) {
36 return
37 }
38 const segments = path.split('.')
39 return function (obj) {
40 for (let i = 0; i < segments.length; i++) {
41 if (!obj) return
42 obj = obj[segments[i]]
43 }
44 return obj
45 }
46}