Skip to content

Commit

Permalink
fix(index): cannot work at Vue 1.0.10 later
Browse files Browse the repository at this point in the history
issues closes #9
  • Loading branch information
kazupon committed Nov 26, 2015
1 parent ff5395a commit 6fd543e
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 4 deletions.
48 changes: 48 additions & 0 deletions src/compare.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Version compare
* - Inspired:
* https://github.com/omichelsen/compare-versions
*/

const PATCH_PATTERN = /-([\w-.]+)/


function split (v) {
let temp = v.split('.')
let arr = temp.splice(0, 2)
arr.push(temp.join('.'))
return arr
}

/**
* compare
*
* @param {String} v1
* @param {String} v2
* @return {Number}
*/

export default function (v1, v2) {
let s1 = split(v1)
let s2 = split(v2)

for (let i = 0; i < 3; i++) {
let n1 = parseInt(s1[i] || 0, 10)
let n2 = parseInt(s2[i] || 0, 10)

if (n1 > n2) { return 1 }
if (n2 > n1) { return -1 }
}

if ((s1[2] + s2[2] + '').indexOf('-') > -1) {
let p1 = (PATCH_PATTERN.exec(s1[2]) || [''])[0]
let p2 = (PATCH_PATTERN.exec(s2[2]) || [''])[0]

if (p1 === '') { return 1 }
if (p2 === '') { return -1 }
if (p1 > p2) { return 1 }
if (p2 > p1) { return -1 }
}

return 0
}
11 changes: 7 additions & 4 deletions src/extend.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import format from './format'
import compare from './compare'


/**
Expand All @@ -10,13 +11,15 @@ import format from './format'
*/

export default function (Vue, locales) {
const path = Vue.parsers.path
const getPath = (Vue.version && compare('1.0.8', Vue.version) === -1)
? Vue.parsers.path.getPath
: Vue.parsers.path.get
const util = Vue.util

function getVal (path, key, lang, args) {
function getVal (key, lang, args) {
let value = key
try {
let val = path.get(locales[lang], key) || locales[lang][key]
let val = getPath(locales[lang], key) || locales[lang][key]
value = (args ? format(val, args) : val) || key
} catch (e) {
value = key
Expand Down Expand Up @@ -51,7 +54,7 @@ export default function (Vue, locales) {
}
}

return getVal(path, key, language, args)
return getVal(key, language, args)
}

return Vue
Expand Down
55 changes: 55 additions & 0 deletions test/specs/compare.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import assert from 'power-assert'
import compare from '../../src/compare'


describe('compare', () => {
it('should compare three-segment versions correctly', () => {
assert.equal(compare('10.1.8', '10.0.4'), 1)
assert.equal(compare('10.0.1', '10.0.1'), 0)
assert.equal(compare('10.1.1', '10.2.2'), -1)
})

it('should compare two-segment versions correctly', () => {
assert.equal(compare('10.8', '10.4'), 1)
assert.equal(compare('10.1', '10.1'), 0)
assert.equal(compare('10.1', '10.2'), -1)
})

it('should compare single-segment versions correctly', () => {
assert.equal(compare('10', '9'), 1)
assert.equal(compare('10', '10'), 0)
assert.equal(compare('9', '10'), -1)
})

it('should compare versions with different number of digits in same group', () => {
assert.equal(compare('11.0.10', '11.0.2'), 1)
assert.equal(compare('11.0.2', '11.0.10'), -1)
})

it('should compare versions with different number of digits in different groups', () => {
assert.equal(compare('11.1.10', '11.0'), 1)
})

it('should compare versions with different number of digits', () => {
assert.equal(compare('1.1.1', '1'), 1)
assert.equal(compare('1.0.0', '1'), 0)
assert.equal(compare('1.0', '1.4.1'), -1)
})

it('should compare pre-release versions', () => {
assert.equal(compare('1.0.0-alpha.1', '1.0.0-alpha'), 1)
assert.equal(compare('1.0.0-alpha', '1.0.0-alpha'), 0)
assert.equal(compare('1.0.0-alpha', '1.0.0-alpha.beta'), -1)
assert.equal(compare('1.0.0-alpha', '1.0.0-beta'), -1)
})

it('should give precendece to normal versions over pre-release', () => {
assert.equal(compare('1.0.0', '1.0.0-alpha'), 1)
assert.equal(compare('1.0.0-beta', '1'), -1)
})

it('should ignore build metadata', () => {
assert.equal(compare('1.4.0-build.3928', '1.4.0-build.3928+sha.a8d9d4f'), 0)
assert.equal(compare('1.4.0-build.3928+sha.b8dbdb0', '1.4.0-build.3928+sha.a8d9d4f'), 0)
})
})

0 comments on commit 6fd543e

Please sign in to comment.