Skip to content

Commit

Permalink
⚡ improvement(vue): support vue 2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Feb 27, 2017
1 parent 3e525d8 commit 5e7bf5e
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 59 deletions.
5 changes: 2 additions & 3 deletions src/format.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* @flow */

import { isNull } from './util'
import { Vue } from './install'
import { isNull, hasOwn } from './util'

export default class BaseFormatter {
_options: FormatterOptions
Expand Down Expand Up @@ -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 ''
}
Expand Down
8 changes: 3 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -16,7 +17,6 @@ export default class VueI18n {
_fallbackRoot: boolean
_fallbackLocale: string
_missing: ?MissingHandler
_getPathValue: Function
_exist: Function

constructor (options: I18nOptions = {}) {
Expand All @@ -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))
Expand Down Expand Up @@ -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 }
Expand Down
82 changes: 38 additions & 44 deletions src/path.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* @flow */

import { isObject, isPlainObject, hasOwn } from './util'

/**
* Path paerser
* - Inspired:
Expand Down Expand Up @@ -278,56 +280,48 @@ export type PathValue = | string | number | boolean | null | PathValueObject | P
export type PathValueObject = Dictionary<PathValue>
export type PathValueArray = Array<PathValue>

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<string> = 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<string> = 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
}
}

35 changes: 31 additions & 4 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/* @flow */

import { Vue } from './install'

/**
* utilites
*/
Expand All @@ -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
}
Expand All @@ -23,7 +50,7 @@ export function parseArgs (...args: Array<mixed>): 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]
Expand All @@ -32,7 +59,7 @@ export function parseArgs (...args: Array<mixed>): 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]
}
}
Expand Down
4 changes: 1 addition & 3 deletions test/unit/path.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import Path from '../../src/path'

const getPathValue = Path(Vue)
import getPathValue from '../../src/path'

describe('path', () => {
describe('primivite', () => {
Expand Down

0 comments on commit 5e7bf5e

Please sign in to comment.