@vue-composable/core
Advanced tools
Comparing version
@@ -41,2 +41,22 @@ 'use strict'; | ||
const isClient = typeof window != "undefined"; | ||
function deepClone(result, ...sources) { | ||
for (let i = 0; i < sources.length; i++) { | ||
const source = sources[i]; | ||
if (source === undefined || !isObject(source)) | ||
continue; | ||
const keys = Object.keys(source); | ||
for (let j = 0; j < keys.length; j++) { | ||
const k = keys[j]; | ||
const v = unwrap(source[k]); | ||
const sourceType = typeof v; | ||
const type = typeof result[k]; | ||
if (result[k] === undefined || sourceType === type) { | ||
result[k] = isObject(v) | ||
? deepClone(result[k] || {}, v) | ||
: source[k]; // source[k] is assigned because if is ref we want to override to this ref | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
// compact version: https://stackoverflow.com/a/33146982/1209882 | ||
@@ -448,4 +468,7 @@ /** | ||
return compositionApi.computed(() => { | ||
const f = unwrap(format); | ||
if (!args) { | ||
return f; | ||
} | ||
const r = compositionApi.isRef(args) ? compositionApi.reactive(args.value) : compositionApi.reactive(args); | ||
const f = unwrap(format); | ||
const regEx = /({?{[\w\s]*}?})/g; | ||
@@ -467,2 +490,44 @@ return f.replace(regEx, s => { | ||
function usePath(source, path, separator = ".", notFoundReturn = NO_OP) { | ||
return compositionApi.computed(() => { | ||
const s = unwrap(source); | ||
const p = unwrap(path); | ||
if (s === undefined) | ||
return notFoundReturn(p, s, p, s); | ||
if (!p) { | ||
return s; | ||
} | ||
const fragments = p.split(separator); | ||
let c = s; | ||
for (let i = 0; i < fragments.length; i++) { | ||
let fragmentPath = fragments[i]; | ||
let index = -1; | ||
if (fragmentPath[fragmentPath.length - 1] === "]") { | ||
const m = fragmentPath.match(/\[(\d+)\]$/); | ||
if (m && m[1]) { | ||
index = +m[1]; | ||
fragmentPath = fragmentPath.slice(0, -m[0].length); | ||
} | ||
} | ||
if (isObject(c)) { | ||
c = c[fragmentPath]; | ||
// array like: when using ref with and array, it becomes arraylike object | ||
if (index >= 0) { | ||
c = c[index]; | ||
} | ||
} | ||
else { | ||
{ | ||
console.warn(`Path "${fragments.slice(0, i).join(separator)}" doesn't exist on:`, source); | ||
} | ||
return notFoundReturn(fragments.slice(0, i).join(separator), c, p, s); | ||
} | ||
if (!c) { | ||
return notFoundReturn(fragments.slice(0, i).join(separator), c, p, s); | ||
} | ||
} | ||
return c; | ||
}); | ||
} | ||
/* /Output */ | ||
@@ -622,6 +687,114 @@ function isValidation(v) { | ||
const I18n_ACCESS_SYMBOL = Symbol(( "I18n") ); | ||
function useI18n(definition) { | ||
if (definition) { | ||
return buildI18n(definition); | ||
} | ||
else | ||
return compositionApi.inject(I18n_ACCESS_SYMBOL); | ||
} | ||
function buildI18n(definition) { | ||
const locales = compositionApi.ref(Object.keys(definition.messages)); | ||
const localeMessages = compositionApi.ref(definition.messages); | ||
const locale = compositionApi.ref(definition.locale); | ||
const i18n = compositionApi.ref({}); | ||
let fallback = compositionApi.ref(); | ||
const cache = {}; | ||
const loadLocale = (locale, localeMessages) => { | ||
if (cache[locale]) { | ||
return cache[locale]; | ||
} | ||
const l = localeMessages.value[locale]; | ||
if (!l) { | ||
return compositionApi.ref({}); | ||
} | ||
if (isFunction(l)) { | ||
return Promise.resolve(l()).then(x => (cache[locale] = wrap(x))); | ||
} | ||
return (cache[locale] = compositionApi.computed(() => localeMessages.value[locale])); | ||
}; | ||
const shouldFallback = definition.fallback | ||
? isBoolean(definition.notFoundFallback) | ||
? definition.notFoundFallback | ||
: true | ||
: false; | ||
let fallbackIsPromise = false; | ||
if (shouldFallback) { | ||
const fallbackI18n = loadLocale(locale.value, localeMessages); | ||
if (isPromise(fallbackI18n)) { | ||
fallbackI18n.then(x => { | ||
fallback = x; | ||
}); | ||
fallbackIsPromise = true; | ||
} | ||
else { | ||
fallback = fallbackI18n; | ||
} | ||
} | ||
else { | ||
fallback.value = {}; | ||
} | ||
compositionApi.watch([locale, fallback], async ([l, fb]) => { | ||
if (l === definition.fallback && shouldFallback) { | ||
i18n.value = fb; | ||
} | ||
else { | ||
const localeMessage = await loadLocale(l, localeMessages); | ||
i18n.value = deepClone({}, fb, localeMessage.value); | ||
} | ||
}, { | ||
lazy: fallbackIsPromise | ||
}); | ||
const $t = (path, args) => { | ||
if (definition.resolve) { | ||
return wrap(definition.resolve(i18n.value, path, args)); | ||
} | ||
return useFormat(usePath(i18n, path, ".", (_, _1, p) => p), args); | ||
}; | ||
const addLocale = (l, m) => { | ||
if (locales.value.indexOf(l) >= 0) { | ||
{ | ||
console.warn("Locale already exists, overriding it"); | ||
} | ||
} | ||
else { | ||
locales.value.push(l); | ||
} | ||
delete cache[l]; | ||
localeMessages.value[l] = m; | ||
}; | ||
const removeLocale = (l) => { | ||
const index = locales.value.indexOf(l); | ||
if (index >= 0) { | ||
locales.value.splice(index, 1); | ||
} | ||
else { | ||
{ | ||
console.warn("Locale doesn't exist"); | ||
} | ||
} | ||
delete localeMessages.value[l]; | ||
delete cache[l]; | ||
}; | ||
return { | ||
locale, | ||
locales, | ||
i18n, | ||
$t, | ||
addLocale, | ||
removeLocale | ||
}; | ||
} | ||
function setI18n(definition) { | ||
const r = buildI18n(definition); | ||
compositionApi.provide(I18n_ACCESS_SYMBOL, r); | ||
return r; | ||
} | ||
exports.FALSE_OP = FALSE_OP; | ||
exports.NO_OP = NO_OP; | ||
exports.PASSIVE_EV = PASSIVE_EV; | ||
exports.buildI18n = buildI18n; | ||
exports.debounce = debounce; | ||
exports.deepClone = deepClone; | ||
exports.exponentialDelay = exponentialDelay; | ||
@@ -642,2 +815,3 @@ exports.isArray = isArray; | ||
exports.promisedTimeout = promisedTimeout; | ||
exports.setI18n = setI18n; | ||
exports.unwrap = unwrap; | ||
@@ -649,4 +823,6 @@ exports.useArrayPagination = useArrayPagination; | ||
exports.useFormat = useFormat; | ||
exports.useI18n = useI18n; | ||
exports.useNow = useNow; | ||
exports.usePagination = usePagination; | ||
exports.usePath = usePath; | ||
exports.usePerformanceNow = usePerformanceNow; | ||
@@ -653,0 +829,0 @@ exports.usePromise = usePromise; |
@@ -41,2 +41,22 @@ 'use strict'; | ||
const isClient = typeof window != "undefined"; | ||
function deepClone(result, ...sources) { | ||
for (let i = 0; i < sources.length; i++) { | ||
const source = sources[i]; | ||
if (source === undefined || !isObject(source)) | ||
continue; | ||
const keys = Object.keys(source); | ||
for (let j = 0; j < keys.length; j++) { | ||
const k = keys[j]; | ||
const v = unwrap(source[k]); | ||
const sourceType = typeof v; | ||
const type = typeof result[k]; | ||
if (result[k] === undefined || sourceType === type) { | ||
result[k] = isObject(v) | ||
? deepClone(result[k] || {}, v) | ||
: source[k]; // source[k] is assigned because if is ref we want to override to this ref | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
// compact version: https://stackoverflow.com/a/33146982/1209882 | ||
@@ -430,4 +450,7 @@ /** | ||
return compositionApi.computed(() => { | ||
const f = unwrap(format); | ||
if (!args) { | ||
return f; | ||
} | ||
const r = compositionApi.isRef(args) ? compositionApi.reactive(args.value) : compositionApi.reactive(args); | ||
const f = unwrap(format); | ||
const regEx = /({?{[\w\s]*}?})/g; | ||
@@ -449,2 +472,41 @@ return f.replace(regEx, s => { | ||
function usePath(source, path, separator = ".", notFoundReturn = NO_OP) { | ||
return compositionApi.computed(() => { | ||
const s = unwrap(source); | ||
const p = unwrap(path); | ||
if (s === undefined) | ||
return notFoundReturn(p, s, p, s); | ||
if (!p) { | ||
return s; | ||
} | ||
const fragments = p.split(separator); | ||
let c = s; | ||
for (let i = 0; i < fragments.length; i++) { | ||
let fragmentPath = fragments[i]; | ||
let index = -1; | ||
if (fragmentPath[fragmentPath.length - 1] === "]") { | ||
const m = fragmentPath.match(/\[(\d+)\]$/); | ||
if (m && m[1]) { | ||
index = +m[1]; | ||
fragmentPath = fragmentPath.slice(0, -m[0].length); | ||
} | ||
} | ||
if (isObject(c)) { | ||
c = c[fragmentPath]; | ||
// array like: when using ref with and array, it becomes arraylike object | ||
if (index >= 0) { | ||
c = c[index]; | ||
} | ||
} | ||
else { | ||
return notFoundReturn(fragments.slice(0, i).join(separator), c, p, s); | ||
} | ||
if (!c) { | ||
return notFoundReturn(fragments.slice(0, i).join(separator), c, p, s); | ||
} | ||
} | ||
return c; | ||
}); | ||
} | ||
/* /Output */ | ||
@@ -604,6 +666,105 @@ function isValidation(v) { | ||
const I18n_ACCESS_SYMBOL = Symbol( ``); | ||
function useI18n(definition) { | ||
if (definition) { | ||
return buildI18n(definition); | ||
} | ||
else | ||
return compositionApi.inject(I18n_ACCESS_SYMBOL); | ||
} | ||
function buildI18n(definition) { | ||
const locales = compositionApi.ref(Object.keys(definition.messages)); | ||
const localeMessages = compositionApi.ref(definition.messages); | ||
const locale = compositionApi.ref(definition.locale); | ||
const i18n = compositionApi.ref({}); | ||
let fallback = compositionApi.ref(); | ||
const cache = {}; | ||
const loadLocale = (locale, localeMessages) => { | ||
if (cache[locale]) { | ||
return cache[locale]; | ||
} | ||
const l = localeMessages.value[locale]; | ||
if (!l) { | ||
return compositionApi.ref({}); | ||
} | ||
if (isFunction(l)) { | ||
return Promise.resolve(l()).then(x => (cache[locale] = wrap(x))); | ||
} | ||
return (cache[locale] = compositionApi.computed(() => localeMessages.value[locale])); | ||
}; | ||
const shouldFallback = definition.fallback | ||
? isBoolean(definition.notFoundFallback) | ||
? definition.notFoundFallback | ||
: true | ||
: false; | ||
let fallbackIsPromise = false; | ||
if (shouldFallback) { | ||
const fallbackI18n = loadLocale(locale.value, localeMessages); | ||
if (isPromise(fallbackI18n)) { | ||
fallbackI18n.then(x => { | ||
fallback = x; | ||
}); | ||
fallbackIsPromise = true; | ||
} | ||
else { | ||
fallback = fallbackI18n; | ||
} | ||
} | ||
else { | ||
fallback.value = {}; | ||
} | ||
compositionApi.watch([locale, fallback], async ([l, fb]) => { | ||
if (l === definition.fallback && shouldFallback) { | ||
i18n.value = fb; | ||
} | ||
else { | ||
const localeMessage = await loadLocale(l, localeMessages); | ||
i18n.value = deepClone({}, fb, localeMessage.value); | ||
} | ||
}, { | ||
lazy: fallbackIsPromise | ||
}); | ||
const $t = (path, args) => { | ||
if (definition.resolve) { | ||
return wrap(definition.resolve(i18n.value, path, args)); | ||
} | ||
return useFormat(usePath(i18n, path, ".", (_, _1, p) => p), args); | ||
}; | ||
const addLocale = (l, m) => { | ||
if (locales.value.indexOf(l) >= 0) ; | ||
else { | ||
locales.value.push(l); | ||
} | ||
delete cache[l]; | ||
localeMessages.value[l] = m; | ||
}; | ||
const removeLocale = (l) => { | ||
const index = locales.value.indexOf(l); | ||
if (index >= 0) { | ||
locales.value.splice(index, 1); | ||
} | ||
delete localeMessages.value[l]; | ||
delete cache[l]; | ||
}; | ||
return { | ||
locale, | ||
locales, | ||
i18n, | ||
$t, | ||
addLocale, | ||
removeLocale | ||
}; | ||
} | ||
function setI18n(definition) { | ||
const r = buildI18n(definition); | ||
compositionApi.provide(I18n_ACCESS_SYMBOL, r); | ||
return r; | ||
} | ||
exports.FALSE_OP = FALSE_OP; | ||
exports.NO_OP = NO_OP; | ||
exports.PASSIVE_EV = PASSIVE_EV; | ||
exports.buildI18n = buildI18n; | ||
exports.debounce = debounce; | ||
exports.deepClone = deepClone; | ||
exports.exponentialDelay = exponentialDelay; | ||
@@ -624,2 +785,3 @@ exports.isArray = isArray; | ||
exports.promisedTimeout = promisedTimeout; | ||
exports.setI18n = setI18n; | ||
exports.unwrap = unwrap; | ||
@@ -631,4 +793,6 @@ exports.useArrayPagination = useArrayPagination; | ||
exports.useFormat = useFormat; | ||
exports.useI18n = useI18n; | ||
exports.useNow = useNow; | ||
exports.usePagination = usePagination; | ||
exports.usePath = usePath; | ||
exports.usePerformanceNow = usePerformanceNow; | ||
@@ -635,0 +799,0 @@ exports.usePromise = usePromise; |
@@ -7,2 +7,4 @@ import { Ref } from '@vue/composition-api'; | ||
export declare function buildI18n<T extends i18nDefinition<TMessage>, TMessage extends Record<keyof T["messages"], i18n | (() => Promise<any>)>>(definition: T): i18nResult<keyof T["messages"], T["messages"][T["locale"]]>; | ||
export declare interface CancellablePromiseResult<TCancel = any> { | ||
@@ -17,2 +19,4 @@ cancel: (result?: TCancel) => void; | ||
export declare function deepClone<T extends object = object>(result: T, ...sources: T[]): T; | ||
declare const ExecutionId: unique symbol; | ||
@@ -30,4 +34,46 @@ | ||
declare type FormatValue = RefTyped<object> | RefTyped<string> | RefTyped<number>; | ||
export declare type FormatValue = RefTyped<object> | RefTyped<string> | RefTyped<number>; | ||
export declare interface i18n extends Record<string, i18nMessageValue> { | ||
} | ||
declare interface i18nDefinition<TMessage> { | ||
locale: keyof TMessage; | ||
fallback?: keyof TMessage; | ||
messages: { | ||
[K in keyof TMessage]: i18n | (() => Promise<i18n>); | ||
}; | ||
/** | ||
* Resolves the translation for i18n | ||
* @param i18n i18n messages | ||
* @param path desired path | ||
* @param args arguments | ||
*/ | ||
resolve?: i18nResolver; | ||
/** | ||
* falls back to the fallback locale if key not found | ||
* @default true | ||
*/ | ||
notFoundFallback?: boolean; | ||
} | ||
declare type i18nLocale<T> = { | ||
[K in keyof T]: i18nMessage<T[K]>; | ||
}; | ||
declare type i18nMessage<T> = T extends Ref<string> ? string : T extends () => Promise<infer P> ? i18nLocale<P> : T extends (...args: infer TArgs) => RefTyped<string> ? (...args: TArgs) => string : T extends object ? i18nLocale<T> : T extends Ref<infer V> ? V : T; | ||
declare type i18nMessageValue = i18nLocale<any> | RefTyped<string>; | ||
declare type i18nResolver = (i18n: i18n, path: Readonly<RefTyped<string>>, args: RefTyped<FormatObject> | Array<FormatValue> | undefined) => RefTyped<string>; | ||
declare interface i18nResult<TLocales, TMessages extends any = i18n> { | ||
locale: Ref<TLocales>; | ||
locales: Ref<Array<TLocales>>; | ||
i18n: Readonly<Ref<Readonly<TMessages>>>; | ||
$t(path: string, args?: object | Array<object>): Readonly<Ref<string>>; | ||
addLocale(locale: string, messages: TMessages): void; | ||
removeLocale(locale: TLocales): void; | ||
} | ||
export declare const isArray: (arg: any) => arg is any[]; | ||
@@ -196,2 +242,4 @@ | ||
export declare function setI18n<T extends i18nDefinition<TMessage>, TMessage extends Record<keyof T["messages"], i18n | (() => Promise<any>)>>(definition: T): i18nResult<keyof T["messages"], T["messages"][T["locale"]]>; | ||
export declare function unwrap(o: RefElement): Element; | ||
@@ -230,6 +278,12 @@ | ||
export declare function useFormat(format: RefTyped<string>, obj: RefTyped<FormatObject>): Readonly<Ref<string>>; | ||
export declare function useFormat(format: Readonly<RefTyped<string>>, obj?: RefTyped<FormatObject>): Readonly<Ref<string>>; | ||
export declare function useFormat(format: RefTyped<string>, ...args: Array<FormatValue>): Readonly<Ref<string>>; | ||
export declare function useFormat(format: Readonly<RefTyped<string>>, ...args: Array<FormatValue>): Readonly<Ref<string>>; | ||
export declare function useFormat(format: Readonly<RefTyped<string>>, obj?: RefTyped<FormatObject> | Array<FormatValue>): Readonly<Ref<string>>; | ||
export declare function useI18n<T extends i18nDefinition<TMessage>, TMessage extends Record<keyof T["messages"], i18n | (() => Promise<any>)>>(definition: T): i18nResult<keyof T["messages"], T["messages"][T["locale"]]>; | ||
export declare function useI18n<T = i18n>(): i18nResult<string[], T> | void; | ||
export declare function useNow(options?: NowOptions & UseNowOptions): { | ||
@@ -249,2 +303,6 @@ now: import("@vue/composition-api").Ref<number>; | ||
export declare function usePath<T extends object = any>(source: RefTyped<T>, path: RefTyped<string>, separator?: string, notFoundReturn?: UsePathNotFoundReturn): Readonly<import("@vue/composition-api").Ref<Readonly<any>>>; | ||
export declare type UsePathNotFoundReturn = (path: string, source: any, fullPath: string, originalSource: any) => any; | ||
export declare function usePerformanceNow(options?: NowOptions): { | ||
@@ -251,0 +309,0 @@ now: import("@vue/composition-api").Ref<number>; |
@@ -1,2 +0,2 @@ | ||
import { isRef, ref, computed, watch, onUnmounted, reactive } from '@vue/composition-api'; | ||
import { isRef, ref, computed, watch, onUnmounted, reactive, inject, provide } from '@vue/composition-api'; | ||
@@ -37,2 +37,22 @@ function unwrap(o) { | ||
const isClient = typeof window != "undefined"; | ||
function deepClone(result, ...sources) { | ||
for (let i = 0; i < sources.length; i++) { | ||
const source = sources[i]; | ||
if (source === undefined || !isObject(source)) | ||
continue; | ||
const keys = Object.keys(source); | ||
for (let j = 0; j < keys.length; j++) { | ||
const k = keys[j]; | ||
const v = unwrap(source[k]); | ||
const sourceType = typeof v; | ||
const type = typeof result[k]; | ||
if (result[k] === undefined || sourceType === type) { | ||
result[k] = isObject(v) | ||
? deepClone(result[k] || {}, v) | ||
: source[k]; // source[k] is assigned because if is ref we want to override to this ref | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
// compact version: https://stackoverflow.com/a/33146982/1209882 | ||
@@ -444,4 +464,7 @@ /** | ||
return computed(() => { | ||
const f = unwrap(format); | ||
if (!args) { | ||
return f; | ||
} | ||
const r = isRef(args) ? reactive(args.value) : reactive(args); | ||
const f = unwrap(format); | ||
const regEx = /({?{[\w\s]*}?})/g; | ||
@@ -463,2 +486,44 @@ return f.replace(regEx, s => { | ||
function usePath(source, path, separator = ".", notFoundReturn = NO_OP) { | ||
return computed(() => { | ||
const s = unwrap(source); | ||
const p = unwrap(path); | ||
if (s === undefined) | ||
return notFoundReturn(p, s, p, s); | ||
if (!p) { | ||
return s; | ||
} | ||
const fragments = p.split(separator); | ||
let c = s; | ||
for (let i = 0; i < fragments.length; i++) { | ||
let fragmentPath = fragments[i]; | ||
let index = -1; | ||
if (fragmentPath[fragmentPath.length - 1] === "]") { | ||
const m = fragmentPath.match(/\[(\d+)\]$/); | ||
if (m && m[1]) { | ||
index = +m[1]; | ||
fragmentPath = fragmentPath.slice(0, -m[0].length); | ||
} | ||
} | ||
if (isObject(c)) { | ||
c = c[fragmentPath]; | ||
// array like: when using ref with and array, it becomes arraylike object | ||
if (index >= 0) { | ||
c = c[index]; | ||
} | ||
} | ||
else { | ||
if ((process.env.NODE_ENV !== 'production')) { | ||
console.warn(`Path "${fragments.slice(0, i).join(separator)}" doesn't exist on:`, source); | ||
} | ||
return notFoundReturn(fragments.slice(0, i).join(separator), c, p, s); | ||
} | ||
if (!c) { | ||
return notFoundReturn(fragments.slice(0, i).join(separator), c, p, s); | ||
} | ||
} | ||
return c; | ||
}); | ||
} | ||
/* /Output */ | ||
@@ -618,2 +683,108 @@ function isValidation(v) { | ||
export { FALSE_OP, NO_OP, PASSIVE_EV, debounce, exponentialDelay, isArray, isBoolean, isClient, isDate, isElement, isFunction, isNumber, isObject, isPromise, isString, isSymbol, minMax, noDelay, promisedTimeout, unwrap, useArrayPagination, useCancellablePromise, useDateNow, useDebounce, useFormat, useNow, usePagination, usePerformanceNow, usePromise, useRetry, useValidation, wrap }; | ||
const I18n_ACCESS_SYMBOL = Symbol(((process.env.NODE_ENV !== 'production') && "I18n") || ``); | ||
function useI18n(definition) { | ||
if (definition) { | ||
return buildI18n(definition); | ||
} | ||
else | ||
return inject(I18n_ACCESS_SYMBOL); | ||
} | ||
function buildI18n(definition) { | ||
const locales = ref(Object.keys(definition.messages)); | ||
const localeMessages = ref(definition.messages); | ||
const locale = ref(definition.locale); | ||
const i18n = ref({}); | ||
let fallback = ref(); | ||
const cache = {}; | ||
const loadLocale = (locale, localeMessages) => { | ||
if (cache[locale]) { | ||
return cache[locale]; | ||
} | ||
const l = localeMessages.value[locale]; | ||
if (!l) { | ||
return ref({}); | ||
} | ||
if (isFunction(l)) { | ||
return Promise.resolve(l()).then(x => (cache[locale] = wrap(x))); | ||
} | ||
return (cache[locale] = computed(() => localeMessages.value[locale])); | ||
}; | ||
const shouldFallback = definition.fallback | ||
? isBoolean(definition.notFoundFallback) | ||
? definition.notFoundFallback | ||
: true | ||
: false; | ||
let fallbackIsPromise = false; | ||
if (shouldFallback) { | ||
const fallbackI18n = loadLocale(locale.value, localeMessages); | ||
if (isPromise(fallbackI18n)) { | ||
fallbackI18n.then(x => { | ||
fallback = x; | ||
}); | ||
fallbackIsPromise = true; | ||
} | ||
else { | ||
fallback = fallbackI18n; | ||
} | ||
} | ||
else { | ||
fallback.value = {}; | ||
} | ||
watch([locale, fallback], async ([l, fb]) => { | ||
if (l === definition.fallback && shouldFallback) { | ||
i18n.value = fb; | ||
} | ||
else { | ||
const localeMessage = await loadLocale(l, localeMessages); | ||
i18n.value = deepClone({}, fb, localeMessage.value); | ||
} | ||
}, { | ||
lazy: fallbackIsPromise | ||
}); | ||
const $t = (path, args) => { | ||
if (definition.resolve) { | ||
return wrap(definition.resolve(i18n.value, path, args)); | ||
} | ||
return useFormat(usePath(i18n, path, ".", (_, _1, p) => p), args); | ||
}; | ||
const addLocale = (l, m) => { | ||
if (locales.value.indexOf(l) >= 0) { | ||
if ((process.env.NODE_ENV !== 'production')) { | ||
console.warn("Locale already exists, overriding it"); | ||
} | ||
} | ||
else { | ||
locales.value.push(l); | ||
} | ||
delete cache[l]; | ||
localeMessages.value[l] = m; | ||
}; | ||
const removeLocale = (l) => { | ||
const index = locales.value.indexOf(l); | ||
if (index >= 0) { | ||
locales.value.splice(index, 1); | ||
} | ||
else { | ||
if ((process.env.NODE_ENV !== 'production')) { | ||
console.warn("Locale doesn't exist"); | ||
} | ||
} | ||
delete localeMessages.value[l]; | ||
delete cache[l]; | ||
}; | ||
return { | ||
locale, | ||
locales, | ||
i18n, | ||
$t, | ||
addLocale, | ||
removeLocale | ||
}; | ||
} | ||
function setI18n(definition) { | ||
const r = buildI18n(definition); | ||
provide(I18n_ACCESS_SYMBOL, r); | ||
return r; | ||
} | ||
export { FALSE_OP, NO_OP, PASSIVE_EV, buildI18n, debounce, deepClone, exponentialDelay, isArray, isBoolean, isClient, isDate, isElement, isFunction, isNumber, isObject, isPromise, isString, isSymbol, minMax, noDelay, promisedTimeout, setI18n, unwrap, useArrayPagination, useCancellablePromise, useDateNow, useDebounce, useFormat, useI18n, useNow, usePagination, usePath, usePerformanceNow, usePromise, useRetry, useValidation, wrap }; |
{ | ||
"name": "@vue-composable/core", | ||
"version": "1.0.0-dev.10", | ||
"version": "1.0.0-dev.11", | ||
"description": "@vue-composable/core", | ||
@@ -36,2 +36,3 @@ "main": "index.js", | ||
"homepage": "https://github.com/pikax/vue-composable/tree/dev/packages/core#readme", | ||
"sideEffects": false, | ||
"devDependencies": { | ||
@@ -38,0 +39,0 @@ "vue": "^2.6.10" |
# @vue-composable/core | ||
<p align="center"><a href="https://pikax.me/vue-composable/" target="_blank" rel="noopener noreferrer"><img width="250" src="https://pikax.me/vue-composable/assets/logo.svg" alt="vue-composable logo"></a></p> | ||
[](https://badge.fury.io/js/%40vue-composable%2Fcore) | ||
[](https://bundlephobia.com/result?p=@vue-composable/core) | ||
<p align="center"> | ||
<br> | ||
<img width="250" src="./logo.svg" alt="logo of vue-composable"> | ||
<br> | ||
<br> | ||
</p> | ||
This package contains core functionality for [vue-composable][vue-composable] | ||
@@ -50,2 +45,6 @@ | ||
### i18n | ||
- [i18n](https://pikax.me/vue-composable/composable/i18n/i18n) - Simple i18n implementation with API inspired by [vue-i18n](https://github.com/kazupon/vue-i18n) | ||
### Format | ||
@@ -52,0 +51,0 @@ |
98850
25.29%2659
25.96%89
-1.11%11
57.14%