diff --git a/src/format.js b/src/format.js index 0b8649510..6472af0a4 100644 --- a/src/format.js +++ b/src/format.js @@ -1,7 +1,6 @@ /* @flow */ -import { isNull } from './util' -import { Vue } from './install' +import { isNull, hasOwn } from './util' export default class BaseFormatter { _options: FormatterOptions @@ -51,7 +50,7 @@ export function template (str: string, ...args: any): string { str[index + match.length] === '}') { return i } else { - result = Vue.util.hasOwn(args, i) ? args[i] : match + result = hasOwn(args, i) ? args[i] : match if (isNull(result)) { return '' } diff --git a/src/index.js b/src/index.js index 15cc31e33..32916d2c4 100644 --- a/src/index.js +++ b/src/index.js @@ -3,7 +3,8 @@ import { install, Vue } from './install' import { warn, isNull, parseArgs, fetchChoice } from './util' import BaseFormatter from './format' -import Path from './path' +import getPathValue from './path' + import type { PathValue } from './path' export default class VueI18n { @@ -16,7 +17,6 @@ export default class VueI18n { _fallbackRoot: boolean _fallbackLocale: string _missing: ?MissingHandler - _getPathValue: Function _exist: Function constructor (options: I18nOptions = {}) { @@ -29,8 +29,6 @@ export default class VueI18n { this._root = options.root || null this._fallbackRoot = options.fallbackRoot || false - const getPathValue: Function = Path(Vue) - this._getPathValue = getPathValue this._exist = (message: Object, key: string): boolean => { if (!message || !key) { return false } return !isNull(getPathValue(message, key)) @@ -83,7 +81,7 @@ export default class VueI18n { _interpolate (message: Messages, key: string, args: any): any { if (!message) { return null } - let val: PathValue = this._getPathValue(message, key) + let val: PathValue = getPathValue(message, key) if (Array.isArray(val)) { return val } if (isNull(val)) { val = message[key] } if (isNull(val)) { return null } diff --git a/src/path.js b/src/path.js index c184ae7ff..ee68e61e7 100644 --- a/src/path.js +++ b/src/path.js @@ -1,5 +1,7 @@ /* @flow */ +import { isObject, isPlainObject, hasOwn } from './util' + /** * Path paerser * - Inspired: @@ -278,56 +280,48 @@ export type PathValue = | string | number | boolean | null | PathValueObject | P export type PathValueObject = Dictionary export type PathValueArray = Array -export default function (Vue: any): Function { - const { isObject, isPlainObject, hasOwn } = Vue.util - - function empty (target: any): boolean { - if (target === null || target === undefined) { return true } +function empty (target: any): boolean { + if (target === null || target === undefined) { return true } - if (Array.isArray(target)) { - if (target.length > 0) { return false } - if (target.length === 0) { return true } - } else if (isPlainObject(target)) { - /* eslint-disable prefer-const */ - for (let key in target) { - if (hasOwn(target, key)) { return false } - } - /* eslint-enable prefer-const */ + if (Array.isArray(target)) { + if (target.length > 0) { return false } + if (target.length === 0) { return true } + } else if (isPlainObject(target)) { + /* eslint-disable prefer-const */ + for (let key in target) { + if (hasOwn(target, key)) { return false } } - - return true + /* eslint-enable prefer-const */ } - /** - * Get path value from path string - */ - - function getPathValue (obj: Object, path: string): PathValue { - if (!isObject(obj)) { return null } + return true +} - const paths: Array = parsePath(path) - if (empty(paths)) { - return null - } else { - const length = paths.length - let ret: any = null - let last: any = obj - let i = 0 - while (i < length) { - const value: any = last[paths[i]] - if (value === undefined) { - last = null - break - } - last = value - i++ +/** + * Get path value from path string + */ +export default function getPathValue (obj: Object, path: string): PathValue { + if (!isObject(obj)) { return null } + + const paths: Array = parsePath(path) + if (empty(paths)) { + return null + } else { + const length = paths.length + let ret: any = null + let last: any = obj + let i = 0 + while (i < length) { + const value: any = last[paths[i]] + if (value === undefined) { + last = null + break } - - ret = last - return ret + last = value + i++ } - } - return getPathValue + ret = last + return ret + } } - diff --git a/src/util.js b/src/util.js index d307f9708..b2dd90184 100644 --- a/src/util.js +++ b/src/util.js @@ -1,7 +1,5 @@ /* @flow */ -import { Vue } from './install' - /** * utilites */ @@ -15,6 +13,35 @@ export function warn (msg: string, err: ?Error): void { } } +const hasOwnProperty = Object.prototype.hasOwnProperty +export function hasOwn (obj: Object, key: string): boolean { + return hasOwnProperty.call(obj, key) +} + +export function bind (fn: Function, ctx: Object): Function { + function boundFn (a) { + const l: number = arguments.length + return l + ? l > 1 + ? fn.apply(ctx, arguments) + : fn.call(ctx, a) + : fn.call(ctx) + } + // record original fn length + boundFn._length = fn.length + return boundFn +} + +export function isObject (obj: mixed): boolean { + return obj !== null && typeof obj === 'object' +} + +const toString = Object.prototype.toString +const OBJECT_STRING = '[object Object]' +export function isPlainObject (obj: any): boolean { + return toString.call(obj) === OBJECT_STRING +} + export function isNull (val: mixed): boolean { return val === null || val === undefined } @@ -23,7 +50,7 @@ export function parseArgs (...args: Array): Object { let locale: ?string = null let params: mixed = null if (args.length === 1) { - if (Vue.util.isObject(args[0]) || Array.isArray(args[0])) { + if (isObject(args[0]) || Array.isArray(args[0])) { params = args[0] } else if (typeof args[0] === 'string') { locale = args[0] @@ -32,7 +59,7 @@ export function parseArgs (...args: Array): Object { if (typeof args[0] === 'string') { locale = args[0] } - if (Vue.util.isObject(args[1]) || Array.isArray(args[1])) { + if (isObject(args[1]) || Array.isArray(args[1])) { params = args[1] } } diff --git a/test/unit/path.test.js b/test/unit/path.test.js index 9fde99749..2885156d5 100644 --- a/test/unit/path.test.js +++ b/test/unit/path.test.js @@ -1,6 +1,4 @@ -import Path from '../../src/path' - -const getPathValue = Path(Vue) +import getPathValue from '../../src/path' describe('path', () => { describe('primivite', () => {