Skip to content

Commit bd35287

Browse files
committed
feat(core): export locale, add mergeLocale prop
1 parent 8042114 commit bd35287

File tree

4 files changed

+59
-10
lines changed

4 files changed

+59
-10
lines changed

core/src/core.vue

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import multiple from './fields/multiple'
33
import types from './types'
44
import locale from './locale'
5+
import util from './util'
56
67
const {getLocale, defaultItems, getSuffix, getPrefix} = locale
78
const {Field} = types
@@ -48,10 +49,12 @@ export default {
4849
customLocale: {
4950
type: Object,
5051
default: function() {
51-
5252
return getLocale(this.locale)
53-
5453
}
54+
},
55+
mergeLocale: {
56+
type: Boolean,
57+
default: true
5558
}
5659
},
5760
data(){
@@ -93,6 +96,15 @@ export default {
9396
return this.computedFields[i]
9497
})
9598
},
99+
computedLocale(){
100+
if(this.mergeLocale){
101+
let defaultLocale = getLocale(this.locale)
102+
return util.deepMerge(defaultLocale, this.customLocale)
103+
}
104+
else{
105+
return this.customLocale
106+
}
107+
}
96108
},
97109
98110
watch: {
@@ -143,11 +155,11 @@ export default {
143155
fieldProps.push({
144156
...field,
145157
cron: this.splitValue[i],
146-
selectedStr: multiple.arrayToStr(values, field).getText(this.customLocale, this.selectedPeriod.id),
158+
selectedStr: multiple.arrayToStr(values, field).getText(this.computedLocale, this.selectedPeriod.id),
147159
events,
148160
attrs,
149-
prefix: getPrefix(this.customLocale, this.selectedPeriod.id, field.id),
150-
suffix: getSuffix(this.customLocale, this.selectedPeriod.id, field.id)
161+
prefix: getPrefix(this.computedLocale, this.selectedPeriod.id, field.id),
162+
suffix: getSuffix(this.computedLocale, this.selectedPeriod.id, field.id)
151163
})
152164
}
153165
@@ -166,8 +178,8 @@ export default {
166178
}
167179
},
168180
items: this.periods,
169-
prefix: this.customLocale.periodPrefix,
170-
suffix: this.customLocale.periodSuffix
181+
prefix: this.computedLocale.periodPrefix,
182+
suffix: this.computedLocale.periodSuffix
171183
}
172184
})
173185
},

core/src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Import vue component
22
import util from './util'
3+
import locale from './locale'
34
import component from './core.vue';
45

56
// Declare install function executed by Vue.use()
@@ -13,7 +14,8 @@ export function install(Vue) {
1314
const plugin = {
1415
install,
1516
component,
16-
util
17+
util,
18+
locale
1719
};
1820

1921
// Auto-install when vue is found (eg. in browser via <script> tag)

core/src/util.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,39 @@ function pad(n, width){
7676
return (n.length < width) ? new Array(width - n.length).fill('0').join('') + n : n;
7777
}
7878

79+
function isObject(obj){
80+
return (obj && typeof obj == 'object' && !Array.isArray(obj))
81+
}
82+
83+
function deepMerge(target, ...sources){
84+
if(!isObject(target) || sources.length == 0) return
85+
let source = sources.shift()
86+
87+
if(isObject(source)){
88+
for(let [key, value] of Object.entries(source)){
89+
if(isObject(value)){
90+
if(!isObject(target[key])){
91+
target[key] = {}
92+
}
93+
deepMerge(target[key], source[key])
94+
}
95+
96+
else {
97+
target[key] = source[key]
98+
}
99+
}
100+
}
101+
102+
if(sources.length > 0) deepMerge(target, sources)
103+
return target
104+
}
105+
79106
export default {
80107
range,
81108
Range,
82109
format,
83110
genItems,
84-
pad
111+
pad,
112+
deepMerge,
113+
isObject
85114
}

core/test/util.test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,10 @@ test('test Range', () => {
1313
expect(r[0]).toBe(0)
1414
expect(r[5]).toBe(10)
1515
expect(Array.from(r)).toEqual([0,2,4,6,8,10])
16-
});
16+
});
17+
18+
test('test deepMerge', () => {
19+
expect(util.deepMerge({ a: { a: 1 } }, { a: { b: 1 }, b: 1 })).toEqual({ a: { a: 1, b: 1 }, b: 1 })
20+
expect(util.deepMerge({}, { a: { b: 1 }})).toEqual({ a: { b: 1 }})
21+
expect(util.deepMerge({ a: { b: 1 }}, { a: { b: 2 }})).toEqual({ a: { b: 2 }})
22+
})

0 commit comments

Comments
 (0)