New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@vue-composable/core

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@vue-composable/core - npm Package Compare versions

Comparing version 1.0.0-dev.2 to 1.0.0-dev.3

230

dist/core.cjs.js

@@ -15,6 +15,5 @@ 'use strict';

const isFunction = (val) => typeof val === "function";
// export const isString = (val: unknown): val is string =>
// typeof val === "string";
// export const isSymbol = (val: unknown): val is symbol =>
// typeof val === "symbol";
const isString = (val) => typeof val === "string";
const isSymbol = (val) => typeof val === "symbol";
const isBoolean = (val) => typeof val === 'boolean';
const isDate = (val) => isObject(val) && isFunction(val.getTime);

@@ -40,106 +39,3 @@ const isNumber = (val) => typeof val === "number";

function usePagination(options) {
const _currentPage = wrap(options.currentPage);
const _pageSize = wrap(options.pageSize);
const _offset = compositionApi.ref(0);
const total = wrap(options.total);
const offset = compositionApi.computed({
get() {
return _offset.value;
},
set(v) {
if (typeof v !== "number") {
/* istanbul ignore else */
{
console.warn(`[offset] expected number but got: '${typeof v}' value: '${v}'`);
}
return;
}
_offset.value = Math.min(v, total.value);
}
});
const currentPage = compositionApi.computed({
get() {
return _currentPage.value;
},
set(v) {
if (typeof v !== "number") {
/* istanbul ignore else */
{
console.warn(`[currentPage] expected number but got: '${typeof v}' value: '${v}'`);
}
return;
}
_currentPage.value = minMax(v, 1, lastPage.value);
// set the offset
offset.value = (_currentPage.value - 1) * pageSize.value;
}
});
const pageSize = compositionApi.computed({
get() {
return _pageSize.value;
},
set(v) {
if (typeof v !== "number") {
/* istanbul ignore else */
{
console.warn(`[pageSize] expected number but got: '${typeof v}' value: '${v}'`);
}
return;
}
_pageSize.value = v;
}
});
const lastPage = compositionApi.computed(() => Math.ceil(total.value / pageSize.value));
// make sure the current page is the correct value
currentPage.value = _currentPage.value;
const prev = () => --currentPage.value;
const next = () => ++currentPage.value;
const first = () => (currentPage.value = 1);
const last = () => (currentPage.value = lastPage.value);
compositionApi.watch([total, pageSize], () => {
if (currentPage.value > lastPage.value) {
currentPage.value = lastPage.value;
}
}, { lazy: true } // no need to run on first render
);
return {
// Mutable state
pageSize,
total,
currentPage,
offset,
// Computed
lastPage,
// Functions
next,
prev,
first,
last
};
}
function useArrayPagination(array, options) {
const arrayRef = wrap(array);
const pagination = usePagination({
...{
currentPage: 1,
pageSize: 10,
},
...options,
total: compositionApi.computed(() => arrayRef.value.length)
});
const result = compositionApi.computed(() => {
const array = arrayRef.value;
if (!Array.isArray(array))
return [];
return array.slice(pagination.offset.value, pagination.offset.value + pagination.pageSize.value);
});
return {
...pagination,
result
};
}
function usePromise(fn) {
function usePromise(fn, throwException = false) {
if (!fn) {

@@ -159,2 +55,3 @@ throw new Error(`[usePromise] argument can't be '${fn}'`);

result.value = null;
let throwExp = args && fn.length !== args.length && args.length > 0 && isBoolean(args[args.length - 1]) ? args[args.length - 1] : throwException;
const currentPromise = (promise.value = fn(...args));

@@ -173,3 +70,3 @@ try {

}
return undefined;
return throwExp ? currentPromise : undefined;
}

@@ -191,3 +88,3 @@ finally {

function useCancellablePromise(fn) {
function useCancellablePromise(fn, throwException = false) {
const cancelled = compositionApi.ref(false);

@@ -203,3 +100,3 @@ let _cancel = undefined;

});
const use = usePromise((...args) => promise(fn(...args)));
const use = usePromise((...args) => promise(fn(...args)), throwException);
return {

@@ -352,6 +249,5 @@ ...use,

function useDebounce(handler, wait) {
return debounce(handler, wait);
function useDebounce(handler, wait, options) {
return debounce(handler, wait, options);
}
/* istanbul ignore next */
function debounce(func, waitMilliseconds = 50, options = {

@@ -380,5 +276,109 @@ isImmediate: false

function usePagination(options) {
const _currentPage = wrap(options.currentPage);
const _pageSize = wrap(options.pageSize);
const _offset = compositionApi.ref(0);
const total = wrap(options.total);
const offset = compositionApi.computed({
get() {
return _offset.value;
},
set(v) {
if (typeof v !== "number") {
/* istanbul ignore else */
{
console.warn(`[offset] expected number but got: '${typeof v}' value: '${v}'`);
}
return;
}
_offset.value = Math.min(v, total.value);
}
});
const currentPage = compositionApi.computed({
get() {
return _currentPage.value;
},
set(v) {
if (typeof v !== "number") {
/* istanbul ignore else */
{
console.warn(`[currentPage] expected number but got: '${typeof v}' value: '${v}'`);
}
return;
}
_currentPage.value = minMax(v, 1, lastPage.value);
// set the offset
offset.value = (_currentPage.value - 1) * pageSize.value;
}
});
const pageSize = compositionApi.computed({
get() {
return _pageSize.value;
},
set(v) {
if (typeof v !== "number") {
/* istanbul ignore else */
{
console.warn(`[pageSize] expected number but got: '${typeof v}' value: '${v}'`);
}
return;
}
_pageSize.value = v;
}
});
const lastPage = compositionApi.computed(() => Math.ceil(total.value / pageSize.value));
// make sure the current page is the correct value
currentPage.value = _currentPage.value;
const prev = () => --currentPage.value;
const next = () => ++currentPage.value;
const first = () => (currentPage.value = 1);
const last = () => (currentPage.value = lastPage.value);
compositionApi.watch([total, pageSize], () => {
if (currentPage.value > lastPage.value) {
currentPage.value = lastPage.value;
}
}, { lazy: true } // no need to run on first render
);
return {
// Mutable state
pageSize,
total,
currentPage,
offset,
// Computed
lastPage,
// Functions
next,
prev,
first,
last
};
}
function useArrayPagination(array, options) {
const arrayRef = wrap(array);
const pagination = usePagination({
...{
currentPage: 1,
pageSize: 10,
},
...options,
total: compositionApi.computed(() => arrayRef.value.length)
});
const result = compositionApi.computed(() => {
const array = arrayRef.value;
if (!Array.isArray(array))
return [];
return array.slice(pagination.offset.value, pagination.offset.value + pagination.pageSize.value);
});
return {
...pagination,
result
};
}
exports.debounce = debounce;
exports.exponentialDelay = exponentialDelay;
exports.isArray = isArray;
exports.isBoolean = isBoolean;
exports.isDate = isDate;

@@ -390,2 +390,4 @@ exports.isElement = isElement;

exports.isPromise = isPromise;
exports.isString = isString;
exports.isSymbol = isSymbol;
exports.minMax = minMax;

@@ -392,0 +394,0 @@ exports.noDelay = noDelay;

@@ -15,6 +15,5 @@ 'use strict';

const isFunction = (val) => typeof val === "function";
// export const isString = (val: unknown): val is string =>
// typeof val === "string";
// export const isSymbol = (val: unknown): val is symbol =>
// typeof val === "symbol";
const isString = (val) => typeof val === "string";
const isSymbol = (val) => typeof val === "symbol";
const isBoolean = (val) => typeof val === 'boolean';
const isDate = (val) => isObject(val) && isFunction(val.getTime);

@@ -40,94 +39,3 @@ const isNumber = (val) => typeof val === "number";

function usePagination(options) {
const _currentPage = wrap(options.currentPage);
const _pageSize = wrap(options.pageSize);
const _offset = compositionApi.ref(0);
const total = wrap(options.total);
const offset = compositionApi.computed({
get() {
return _offset.value;
},
set(v) {
if (typeof v !== "number") {
return;
}
_offset.value = Math.min(v, total.value);
}
});
const currentPage = compositionApi.computed({
get() {
return _currentPage.value;
},
set(v) {
if (typeof v !== "number") {
return;
}
_currentPage.value = minMax(v, 1, lastPage.value);
// set the offset
offset.value = (_currentPage.value - 1) * pageSize.value;
}
});
const pageSize = compositionApi.computed({
get() {
return _pageSize.value;
},
set(v) {
if (typeof v !== "number") {
return;
}
_pageSize.value = v;
}
});
const lastPage = compositionApi.computed(() => Math.ceil(total.value / pageSize.value));
// make sure the current page is the correct value
currentPage.value = _currentPage.value;
const prev = () => --currentPage.value;
const next = () => ++currentPage.value;
const first = () => (currentPage.value = 1);
const last = () => (currentPage.value = lastPage.value);
compositionApi.watch([total, pageSize], () => {
if (currentPage.value > lastPage.value) {
currentPage.value = lastPage.value;
}
}, { lazy: true } // no need to run on first render
);
return {
// Mutable state
pageSize,
total,
currentPage,
offset,
// Computed
lastPage,
// Functions
next,
prev,
first,
last
};
}
function useArrayPagination(array, options) {
const arrayRef = wrap(array);
const pagination = usePagination({
...{
currentPage: 1,
pageSize: 10,
},
...options,
total: compositionApi.computed(() => arrayRef.value.length)
});
const result = compositionApi.computed(() => {
const array = arrayRef.value;
if (!Array.isArray(array))
return [];
return array.slice(pagination.offset.value, pagination.offset.value + pagination.pageSize.value);
});
return {
...pagination,
result
};
}
function usePromise(fn) {
function usePromise(fn, throwException = false) {
if (!fn) {

@@ -147,2 +55,3 @@ throw new Error(`[usePromise] argument can't be '${fn}'`);

result.value = null;
let throwExp = args && fn.length !== args.length && args.length > 0 && isBoolean(args[args.length - 1]) ? args[args.length - 1] : throwException;
const currentPromise = (promise.value = fn(...args));

@@ -161,3 +70,3 @@ try {

}
return undefined;
return throwExp ? currentPromise : undefined;
}

@@ -179,3 +88,3 @@ finally {

function useCancellablePromise(fn) {
function useCancellablePromise(fn, throwException = false) {
const cancelled = compositionApi.ref(false);

@@ -191,3 +100,3 @@ let _cancel = undefined;

});
const use = usePromise((...args) => promise(fn(...args)));
const use = usePromise((...args) => promise(fn(...args)), throwException);
return {

@@ -340,6 +249,5 @@ ...use,

function useDebounce(handler, wait) {
return debounce(handler, wait);
function useDebounce(handler, wait, options) {
return debounce(handler, wait, options);
}
/* istanbul ignore next */
function debounce(func, waitMilliseconds = 50, options = {

@@ -368,5 +276,97 @@ isImmediate: false

function usePagination(options) {
const _currentPage = wrap(options.currentPage);
const _pageSize = wrap(options.pageSize);
const _offset = compositionApi.ref(0);
const total = wrap(options.total);
const offset = compositionApi.computed({
get() {
return _offset.value;
},
set(v) {
if (typeof v !== "number") {
return;
}
_offset.value = Math.min(v, total.value);
}
});
const currentPage = compositionApi.computed({
get() {
return _currentPage.value;
},
set(v) {
if (typeof v !== "number") {
return;
}
_currentPage.value = minMax(v, 1, lastPage.value);
// set the offset
offset.value = (_currentPage.value - 1) * pageSize.value;
}
});
const pageSize = compositionApi.computed({
get() {
return _pageSize.value;
},
set(v) {
if (typeof v !== "number") {
return;
}
_pageSize.value = v;
}
});
const lastPage = compositionApi.computed(() => Math.ceil(total.value / pageSize.value));
// make sure the current page is the correct value
currentPage.value = _currentPage.value;
const prev = () => --currentPage.value;
const next = () => ++currentPage.value;
const first = () => (currentPage.value = 1);
const last = () => (currentPage.value = lastPage.value);
compositionApi.watch([total, pageSize], () => {
if (currentPage.value > lastPage.value) {
currentPage.value = lastPage.value;
}
}, { lazy: true } // no need to run on first render
);
return {
// Mutable state
pageSize,
total,
currentPage,
offset,
// Computed
lastPage,
// Functions
next,
prev,
first,
last
};
}
function useArrayPagination(array, options) {
const arrayRef = wrap(array);
const pagination = usePagination({
...{
currentPage: 1,
pageSize: 10,
},
...options,
total: compositionApi.computed(() => arrayRef.value.length)
});
const result = compositionApi.computed(() => {
const array = arrayRef.value;
if (!Array.isArray(array))
return [];
return array.slice(pagination.offset.value, pagination.offset.value + pagination.pageSize.value);
});
return {
...pagination,
result
};
}
exports.debounce = debounce;
exports.exponentialDelay = exponentialDelay;
exports.isArray = isArray;
exports.isBoolean = isBoolean;
exports.isDate = isDate;

@@ -378,2 +378,4 @@ exports.isElement = isElement;

exports.isPromise = isPromise;
exports.isString = isString;
exports.isSymbol = isSymbol;
exports.minMax = minMax;

@@ -380,0 +382,0 @@ exports.noDelay = noDelay;

import { Ref } from '@vue/composition-api';
import { RefTyped as RefTyped_2 } from '@vue-composable/core';

@@ -7,4 +8,4 @@ export declare interface ArrayPaginationResult<T extends Array<any>> extends PaginationResult {

export declare interface CancellablePromiseResult<TR> {
cancel: (result?: TR) => void;
export declare interface CancellablePromiseResult<TCancel = any> {
cancel: (result?: TCancel) => void;
cancelled: Ref<boolean>;

@@ -25,2 +26,4 @@ }

export declare const isBoolean: (val: unknown) => val is Boolean;
export declare const isDate: (val: unknown) => val is Date;

@@ -38,2 +41,6 @@

export declare const isString: (val: unknown) => val is string;
export declare const isSymbol: (val: unknown) => val is symbol;
export declare function minMax(val: number, min: number, max: number): number;

@@ -50,5 +57,5 @@

export declare interface PaginationOptions {
pageSize: RefTyped<number>;
total: RefTyped<number>;
currentPage: RefTyped<number>;
pageSize: RefTyped_2<number>;
total: RefTyped_2<number>;
currentPage: RefTyped_2<number>;
}

@@ -83,3 +90,3 @@

export declare interface PromiseResultFactory<T extends Promise<any>, TArgs extends Array<any> = never> extends PromiseResult<T> {
export declare interface PromiseResultFactory<T extends Promise<any>, TArgs extends Array<any> = Array<any>> extends PromiseResult<T> {
exec: (...args: TArgs) => Promise<PromiseType<T> | undefined>;

@@ -171,16 +178,47 @@ }

export declare function useArrayPagination<T extends Array<TR>, TR>(array: RefTyped<T>, options?: Partial<Omit<PaginationOptions, 'total'>>): ArrayPaginationResult<T>;
export declare function useArrayPagination<T extends Array<TR>, TR>(array: RefTyped_2<T>, options?: Partial<Omit<PaginationOptions, 'total'>>): ArrayPaginationResult<T>;
export declare function useCancellablePromise<T extends Promise<TR>, TR, TArgs extends Array<any>>(fn: (...args: TArgs) => T): PromiseResultFactory<T, TArgs> & CancellablePromiseResult<TR>;
export declare function useCancellablePromise<T extends any, TArgs extends Array<any>>(fn: (...args: TArgs) => Promise<T>): PromiseResultFactory<Promise<T>, TArgs> & CancellablePromiseResult;
export declare function useCancellablePromise<T extends Promise<TR>, TR>(fn: () => T): PromiseResultFactory<T> & CancellablePromiseResult<TR>;
export declare function useCancellablePromise<T extends any, TArgs extends Array<any>>(fn: (...args: TArgs) => Promise<T>, throwException: boolean): PromiseResultFactory<Promise<T>, TArgs> & CancellablePromiseResult;
export declare function useDebounce<T extends Function>(handler: T, wait?: number): T;
export declare function useCancellablePromise<T extends any>(fn: () => T): PromiseResultFactory<Promise<T>> & CancellablePromiseResult;
export declare function useCancellablePromise<T extends any>(fn: () => T, throwException: boolean): PromiseResultFactory<Promise<T>> & CancellablePromiseResult;
export declare function useCancellablePromise<T extends Promise<TR>, TR, TArgs extends Array<any>>(fn: (...args: TArgs) => T): PromiseResultFactory<T, TArgs> & CancellablePromiseResult;
export declare function useCancellablePromise<T extends Promise<TR>, TR, TArgs extends Array<any>>(fn: (...args: TArgs) => T, throwException: boolean): PromiseResultFactory<T, TArgs> & CancellablePromiseResult;
export declare function useCancellablePromise<T = any>(fn: () => T): PromiseResultFactory<Promise<T>> & CancellablePromiseResult;
export declare function useCancellablePromise<T = any>(fn: () => T, throwException: boolean): PromiseResultFactory<Promise<T>> & CancellablePromiseResult;
export declare function useCancellablePromise<T extends Promise<TR>, TR>(fn: () => T): PromiseResultFactory<T> & CancellablePromiseResult;
export declare function useDebounce<T extends Function>(handler: T, wait?: number, options?: Options): T;
export declare function usePagination(options: PaginationOptions): PaginationResult;
export declare function usePromise<T extends Promise<any>, TArgs extends Array<any>>(fn: (...args: TArgs) => T): PromiseResultFactory<T, TArgs>;
/**
*
* @param fn - factory function
* @param throwException - if `true` allows to catch exception when `exec()`
*/
export declare function usePromise<T = any, TArgs extends Array<any> = Array<any>>(fn: (...args: TArgs) => Promise<T>, throwException?: boolean): PromiseResultFactory<Promise<T>, TArgs>;
export declare function usePromise<T extends Promise<any>>(fn: () => T): PromiseResultFactory<T>;
export declare function usePromise<T = any, TArgs extends Array<any> = Array<any>>(fn: (...args: TArgs) => Promise<T>): PromiseResultFactory<Promise<T>, TArgs>;
export declare function usePromise<T = any, TArgs extends Array<any> = Array<any>>(fn: (...args: TArgs) => T, throwException: boolean): PromiseResultFactory<Promise<T>, TArgs>;
export declare function usePromise<T = any, TArgs extends Array<any> = Array<any>>(fn: (...args: TArgs) => T): PromiseResultFactory<Promise<T>, TArgs>;
export declare function usePromise<T = any>(fn: () => Promise<T>, throwException: boolean): PromiseResultFactory<Promise<T>>;
export declare function usePromise<T = any>(fn: () => Promise<T>): PromiseResultFactory<Promise<T>>;
export declare function usePromise<T = any>(fn: () => T, throwException: boolean): PromiseResultFactory<Promise<T>>;
export declare function usePromise<T = any>(fn: () => T): PromiseResultFactory<Promise<T>>;
export declare function useRetry(options?: RetryOptions): RetryReturnNoFactory;

@@ -187,0 +225,0 @@

@@ -11,6 +11,5 @@ import { isRef, ref, computed, watch } from '@vue/composition-api';

const isFunction = (val) => typeof val === "function";
// export const isString = (val: unknown): val is string =>
// typeof val === "string";
// export const isSymbol = (val: unknown): val is symbol =>
// typeof val === "symbol";
const isString = (val) => typeof val === "string";
const isSymbol = (val) => typeof val === "symbol";
const isBoolean = (val) => typeof val === 'boolean';
const isDate = (val) => isObject(val) && isFunction(val.getTime);

@@ -36,106 +35,3 @@ const isNumber = (val) => typeof val === "number";

function usePagination(options) {
const _currentPage = wrap(options.currentPage);
const _pageSize = wrap(options.pageSize);
const _offset = ref(0);
const total = wrap(options.total);
const offset = computed({
get() {
return _offset.value;
},
set(v) {
if (typeof v !== "number") {
/* istanbul ignore else */
if ((true !== 'production')) {
console.warn(`[offset] expected number but got: '${typeof v}' value: '${v}'`);
}
return;
}
_offset.value = Math.min(v, total.value);
}
});
const currentPage = computed({
get() {
return _currentPage.value;
},
set(v) {
if (typeof v !== "number") {
/* istanbul ignore else */
if ((true !== 'production')) {
console.warn(`[currentPage] expected number but got: '${typeof v}' value: '${v}'`);
}
return;
}
_currentPage.value = minMax(v, 1, lastPage.value);
// set the offset
offset.value = (_currentPage.value - 1) * pageSize.value;
}
});
const pageSize = computed({
get() {
return _pageSize.value;
},
set(v) {
if (typeof v !== "number") {
/* istanbul ignore else */
if ((true !== 'production')) {
console.warn(`[pageSize] expected number but got: '${typeof v}' value: '${v}'`);
}
return;
}
_pageSize.value = v;
}
});
const lastPage = computed(() => Math.ceil(total.value / pageSize.value));
// make sure the current page is the correct value
currentPage.value = _currentPage.value;
const prev = () => --currentPage.value;
const next = () => ++currentPage.value;
const first = () => (currentPage.value = 1);
const last = () => (currentPage.value = lastPage.value);
watch([total, pageSize], () => {
if (currentPage.value > lastPage.value) {
currentPage.value = lastPage.value;
}
}, { lazy: true } // no need to run on first render
);
return {
// Mutable state
pageSize,
total,
currentPage,
offset,
// Computed
lastPage,
// Functions
next,
prev,
first,
last
};
}
function useArrayPagination(array, options) {
const arrayRef = wrap(array);
const pagination = usePagination({
...{
currentPage: 1,
pageSize: 10,
},
...options,
total: computed(() => arrayRef.value.length)
});
const result = computed(() => {
const array = arrayRef.value;
if (!Array.isArray(array))
return [];
return array.slice(pagination.offset.value, pagination.offset.value + pagination.pageSize.value);
});
return {
...pagination,
result
};
}
function usePromise(fn) {
function usePromise(fn, throwException = false) {
if (!fn) {

@@ -155,2 +51,3 @@ throw new Error(`[usePromise] argument can't be '${fn}'`);

result.value = null;
let throwExp = args && fn.length !== args.length && args.length > 0 && isBoolean(args[args.length - 1]) ? args[args.length - 1] : throwException;
const currentPromise = (promise.value = fn(...args));

@@ -169,3 +66,3 @@ try {

}
return undefined;
return throwExp ? currentPromise : undefined;
}

@@ -187,3 +84,3 @@ finally {

function useCancellablePromise(fn) {
function useCancellablePromise(fn, throwException = false) {
const cancelled = ref(false);

@@ -199,3 +96,3 @@ let _cancel = undefined;

});
const use = usePromise((...args) => promise(fn(...args)));
const use = usePromise((...args) => promise(fn(...args)), throwException);
return {

@@ -348,6 +245,5 @@ ...use,

function useDebounce(handler, wait) {
return debounce(handler, wait);
function useDebounce(handler, wait, options) {
return debounce(handler, wait, options);
}
/* istanbul ignore next */
function debounce(func, waitMilliseconds = 50, options = {

@@ -376,2 +272,105 @@ isImmediate: false

export { debounce, exponentialDelay, isArray, isDate, isElement, isFunction, isNumber, isObject, isPromise, minMax, noDelay, promisedTimeout, unwrap, useArrayPagination, useCancellablePromise, useDebounce, usePagination, usePromise, useRetry, wrap };
function usePagination(options) {
const _currentPage = wrap(options.currentPage);
const _pageSize = wrap(options.pageSize);
const _offset = ref(0);
const total = wrap(options.total);
const offset = computed({
get() {
return _offset.value;
},
set(v) {
if (typeof v !== "number") {
/* istanbul ignore else */
if ((true !== 'production')) {
console.warn(`[offset] expected number but got: '${typeof v}' value: '${v}'`);
}
return;
}
_offset.value = Math.min(v, total.value);
}
});
const currentPage = computed({
get() {
return _currentPage.value;
},
set(v) {
if (typeof v !== "number") {
/* istanbul ignore else */
if ((true !== 'production')) {
console.warn(`[currentPage] expected number but got: '${typeof v}' value: '${v}'`);
}
return;
}
_currentPage.value = minMax(v, 1, lastPage.value);
// set the offset
offset.value = (_currentPage.value - 1) * pageSize.value;
}
});
const pageSize = computed({
get() {
return _pageSize.value;
},
set(v) {
if (typeof v !== "number") {
/* istanbul ignore else */
if ((true !== 'production')) {
console.warn(`[pageSize] expected number but got: '${typeof v}' value: '${v}'`);
}
return;
}
_pageSize.value = v;
}
});
const lastPage = computed(() => Math.ceil(total.value / pageSize.value));
// make sure the current page is the correct value
currentPage.value = _currentPage.value;
const prev = () => --currentPage.value;
const next = () => ++currentPage.value;
const first = () => (currentPage.value = 1);
const last = () => (currentPage.value = lastPage.value);
watch([total, pageSize], () => {
if (currentPage.value > lastPage.value) {
currentPage.value = lastPage.value;
}
}, { lazy: true } // no need to run on first render
);
return {
// Mutable state
pageSize,
total,
currentPage,
offset,
// Computed
lastPage,
// Functions
next,
prev,
first,
last
};
}
function useArrayPagination(array, options) {
const arrayRef = wrap(array);
const pagination = usePagination({
...{
currentPage: 1,
pageSize: 10,
},
...options,
total: computed(() => arrayRef.value.length)
});
const result = computed(() => {
const array = arrayRef.value;
if (!Array.isArray(array))
return [];
return array.slice(pagination.offset.value, pagination.offset.value + pagination.pageSize.value);
});
return {
...pagination,
result
};
}
export { debounce, exponentialDelay, isArray, isBoolean, isDate, isElement, isFunction, isNumber, isObject, isPromise, isString, isSymbol, minMax, noDelay, promisedTimeout, unwrap, useArrayPagination, useCancellablePromise, useDebounce, usePagination, usePromise, useRetry, wrap };

@@ -12,6 +12,5 @@ var VueComposableCore = (function (exports, compositionApi) {

const isFunction = (val) => typeof val === "function";
// export const isString = (val: unknown): val is string =>
// typeof val === "string";
// export const isSymbol = (val: unknown): val is symbol =>
// typeof val === "symbol";
const isString = (val) => typeof val === "string";
const isSymbol = (val) => typeof val === "symbol";
const isBoolean = (val) => typeof val === 'boolean';
const isDate = (val) => isObject(val) && isFunction(val.getTime);

@@ -37,7 +36,30 @@ const isNumber = (val) => typeof val === "number";

function wrap$1(o) {
return compositionApi.isRef(o) ? o : compositionApi.ref(o);
}
const isFunction$1 = (val) => typeof val === "function";
const isDate$1 = (val) => isObject$1(val) && isFunction$1(val.getTime);
const isNumber$1 = (val) => typeof val === "number";
const isObject$1 = (val) => val !== null && typeof val === "object";
function isPromise$1(val) {
return isObject$1(val) && isFunction$1(val.then) && isFunction$1(val.catch);
}
function promisedTimeout$1(timeout) {
return new Promise(res => {
setTimeout(res, timeout);
});
}
function minMax$1(val, min, max) {
if (val < min)
return min;
if (val > max)
return max;
return val;
}
function usePagination(options) {
const _currentPage = wrap(options.currentPage);
const _pageSize = wrap(options.pageSize);
const _currentPage = wrap$1(options.currentPage);
const _pageSize = wrap$1(options.pageSize);
const _offset = compositionApi.ref(0);
const total = wrap(options.total);
const total = wrap$1(options.total);
const offset = compositionApi.computed({

@@ -70,3 +92,3 @@ get() {

}
_currentPage.value = minMax(v, 1, lastPage.value);
_currentPage.value = minMax$1(v, 1, lastPage.value);
// set the offset

@@ -121,3 +143,3 @@ offset.value = (_currentPage.value - 1) * pageSize.value;

function useArrayPagination(array, options) {
const arrayRef = wrap(array);
const arrayRef = wrap$1(array);
const pagination = usePagination({

@@ -143,3 +165,3 @@ ...{

function usePromise(fn) {
function usePromise(fn, throwException = false) {
if (!fn) {

@@ -159,2 +181,3 @@ throw new Error(`[usePromise] argument can't be '${fn}'`);

result.value = null;
let throwExp = args && fn.length !== args.length && args.length > 0 && isBoolean(args[args.length - 1]) ? args[args.length - 1] : throwException;
const currentPromise = (promise.value = fn(...args));

@@ -173,3 +196,3 @@ try {

}
return undefined;
return throwExp ? currentPromise : undefined;
}

@@ -191,3 +214,3 @@ finally {

function useCancellablePromise(fn) {
function useCancellablePromise(fn, throwException = false) {
const cancelled = compositionApi.ref(false);

@@ -203,3 +226,3 @@ let _cancel = undefined;

});
const use = usePromise((...args) => promise(fn(...args)));
const use = usePromise((...args) => promise(fn(...args)), throwException);
return {

@@ -237,3 +260,3 @@ ...use,

}
if (isPromise(result)) {
if (isPromise$1(result)) {
result = await result;

@@ -268,8 +291,8 @@ }

const pDelayBy = delay(i); // wrapped
const delayBy = isPromise(pDelayBy) ? await pDelayBy : pDelayBy; // unwrap promise
if (!isPromise(pDelayBy) || !!delayBy) {
if (isNumber(delayBy)) {
const delayBy = isPromise$1(pDelayBy) ? await pDelayBy : pDelayBy; // unwrap promise
if (!isPromise$1(pDelayBy) || !!delayBy) {
if (isNumber$1(delayBy)) {
nextRetry = delayBy;
}
else if (isDate(delayBy)) {
else if (isDate$1(delayBy)) {
nextRetry = delayBy.getTime();

@@ -289,3 +312,3 @@ }

if (nextRetry > 0) {
await promisedTimeout(nextRetry);
await promisedTimeout$1(nextRetry);
}

@@ -305,8 +328,8 @@ }

function useRetry(options, factory) {
const opt = !options || isFunction(options) ? {} : options;
const fn = isFunction(options) ? options : factory;
if (!isFunction(options) && !isObject(options)) {
const opt = !options || isFunction$1(options) ? {} : options;
const fn = isFunction$1(options) ? options : factory;
if (!isFunction$1(options) && !isObject$1(options)) {
throw new Error("[useRetry] options needs to be 'object'");
}
if (!!fn && !isFunction(fn)) {
if (!!fn && !isFunction$1(fn)) {
throw new Error("[useRetry] factory needs to be 'function'");

@@ -356,6 +379,5 @@ }

function useDebounce(handler, wait) {
return debounce(handler, wait);
function useDebounce(handler, wait, options) {
return debounce(handler, wait, options);
}
/* istanbul ignore next */
function debounce(func, waitMilliseconds = 50, options = {

@@ -387,2 +409,3 @@ isImmediate: false

exports.isArray = isArray;
exports.isBoolean = isBoolean;
exports.isDate = isDate;

@@ -394,2 +417,4 @@ exports.isElement = isElement;

exports.isPromise = isPromise;
exports.isString = isString;
exports.isSymbol = isSymbol;
exports.minMax = minMax;

@@ -396,0 +421,0 @@ exports.noDelay = noDelay;

@@ -1,1 +0,1 @@

var VueComposableCore=function(e,t){"use strict";function r(e){return t.isRef(e)?e:t.ref(e)}const u=Array.isArray,n=e=>"function"==typeof e,a=e=>o(e)&&n(e.getTime),l=e=>"number"==typeof e,o=e=>null!==e&&"object"==typeof e;function i(e){return o(e)&&n(e.then)&&n(e.catch)}function s(e){return new Promise(t=>{setTimeout(t,e)})}function v(e,t,r){return e<t?t:e>r?r:e}function c(e){const u=r(e.currentPage),n=r(e.pageSize),a=t.ref(0),l=r(e.total),o=t.computed({get:()=>a.value,set(e){"number"==typeof e&&(a.value=Math.min(e,l.value))}}),i=t.computed({get:()=>u.value,set(e){"number"==typeof e&&(u.value=v(e,1,c.value),o.value=(u.value-1)*s.value)}}),s=t.computed({get:()=>n.value,set(e){"number"==typeof e&&(n.value=e)}}),c=t.computed(()=>Math.ceil(l.value/s.value));i.value=u.value;return t.watch([l,s],()=>{i.value>c.value&&(i.value=c.value)},{lazy:!0}),{pageSize:s,total:l,currentPage:i,offset:o,lastPage:c,next:()=>++i.value,prev:()=>--i.value,first:()=>i.value=1,last:()=>i.value=c.value}}function f(e){if(!e)throw new Error(`[usePromise] argument can't be '${e}'`);if("function"!=typeof e)throw new Error(`[usePromise] expects function, but received ${typeof e}`);const r=t.ref(!1),u=t.ref(null),n=t.ref(null),a=t.ref();return{exec:async(...t)=>{r.value=!0,u.value=null,n.value=null;const l=a.value=e(...t);try{const e=await l;return a.value===l&&(n.value=e),e}catch(e){return void(a.value===l&&(u.value=e,n.value=null))}finally{a.value===l&&(r.value=!1)}},result:n,promise:a,loading:r,error:u}}const y=Symbol(void 0),m=Symbol(void 0),p=async(e,t,r,u)=>{const n=t[y].value;let o=-1;const v=e.maxRetries||9001,c=e.retryDelay||d;t.retryErrors.value=[],t.isRetrying.value=!1,t.nextRetry.value=void 0;let f=void 0;do{let e=!1,p=null;try{if(++o,p=u?r(...u):r(),i(p)&&(p=await p),t[m].value)return null;e=!0}catch(e){p=null,t.retryErrors.value.push(e)}if(n!==t[y].value)return p;if(e)return t.isRetrying.value=!1,t.nextRetry.value=void 0,p;if(o>=v)return t.isRetrying.value=!1,t.nextRetry.value=void 0,Promise.reject(new Error(`[useRetry] max retries reached ${v}`));t.isRetrying.value=!0;const d=Date.now(),g=c(o),w=i(g)?await g:g;if(!i(g)||w){if(l(w))f=w;else{if(!a(w))throw new Error(`[useRetry] invalid value received from options.retryDelay '${typeof w}'`);f=w.getTime()}f<d?t.nextRetry.value=d+f:(t.nextRetry.value=f,f-=d),f>0&&await s(f)}if(t[m].value)return null;if(n!==t[y].value)return p}while(o<9e3);return null};const d=()=>0;function g(e,t=50,r={isImmediate:!1}){let u;return function(...n){const a=this,l=r.isImmediate&&void 0===u;void 0!==u&&clearTimeout(u),u=setTimeout((function(){u=void 0,r.isImmediate||e.apply(a,n)}),t),l&&e.apply(a,n)}}return e.debounce=g,e.exponentialDelay=e=>{const t=100*Math.pow(2,e);return t+.2*t*Math.random()},e.isArray=u,e.isDate=a,e.isElement=e=>o(e)&&!!e.tagName,e.isFunction=n,e.isNumber=l,e.isObject=o,e.isPromise=i,e.minMax=v,e.noDelay=d,e.promisedTimeout=s,e.unwrap=function(e){return t.isRef(e)?e.value:e},e.useArrayPagination=function(e,u){const n=r(e),a=c({currentPage:1,pageSize:10,...u,total:t.computed(()=>n.value.length)}),l=t.computed(()=>{const e=n.value;return Array.isArray(e)?e.slice(a.offset.value,a.offset.value+a.pageSize.value):[]});return{...a,result:l}},e.useCancellablePromise=function(e){const r=t.ref(!1);let u=void 0;return{...f((...t)=>(e=>new Promise((t,n)=>{u=e=>{r.value=!0,n(e)},e.then(t).catch(n)}))(e(...t))),cancel:e=>u(e),cancelled:r}},e.useDebounce=function(e,t){return g(e,t)},e.usePagination=c,e.usePromise=f,e.useRetry=function(e,r){const u=!e||n(e)?{}:e,a=n(e)?e:r;if(!n(e)&&!o(e))throw new Error("[useRetry] options needs to be 'object'");if(a&&!n(a))throw new Error("[useRetry] factory needs to be 'function'");const l=t.ref(!1),i=t.ref(),s=t.ref([]),v={value:!1},c={isRetrying:l,retryCount:t.computed(()=>s.value.length),nextRetry:i,retryErrors:s,[y]:{value:0},[m]:v},f=a?(...e)=>(++c[y].value,p(u,c,a,e)):e=>(++c[y].value,p(u,c,e,void 0));return{...c,cancel:()=>{c.isRetrying.value=!1,c.retryErrors.value.push(new Error("[useRetry] cancelled")),c.nextRetry.value=void 0,v.value=!0},exec:f}},e.wrap=r,e}({},vueCompositionApi);
var VueComposableCore=function(e,t){"use strict";const r=Array.isArray,n=e=>"function"==typeof e,u=e=>"boolean"==typeof e,o=e=>null!==e&&"object"==typeof e;function a(e){return t.isRef(e)?e:t.ref(e)}const l=e=>"function"==typeof e,i=e=>c(e)&&l(e.getTime),s=e=>"number"==typeof e,c=e=>null!==e&&"object"==typeof e;function v(e){return c(e)&&l(e.then)&&l(e.catch)}function f(e){return new Promise(t=>{setTimeout(t,e)})}function y(e){const r=a(e.currentPage),n=a(e.pageSize),u=t.ref(0),o=a(e.total),l=t.computed({get:()=>u.value,set(e){"number"==typeof e&&(u.value=Math.min(e,o.value))}}),i=t.computed({get:()=>r.value,set(e){var t,n,u;"number"==typeof e&&(r.value=(t=e,n=1,u=c.value,t<n?n:t>u?u:t),l.value=(r.value-1)*s.value)}}),s=t.computed({get:()=>n.value,set(e){"number"==typeof e&&(n.value=e)}}),c=t.computed(()=>Math.ceil(o.value/s.value));i.value=r.value;return t.watch([o,s],()=>{i.value>c.value&&(i.value=c.value)},{lazy:!0}),{pageSize:s,total:o,currentPage:i,offset:l,lastPage:c,next:()=>++i.value,prev:()=>--i.value,first:()=>i.value=1,last:()=>i.value=c.value}}function m(e,r=!1){if(!e)throw new Error(`[usePromise] argument can't be '${e}'`);if("function"!=typeof e)throw new Error(`[usePromise] expects function, but received ${typeof e}`);const n=t.ref(!1),o=t.ref(null),a=t.ref(null),l=t.ref();return{exec:async(...t)=>{n.value=!0,o.value=null,a.value=null;let i=t&&e.length!==t.length&&t.length>0&&u(t[t.length-1])?t[t.length-1]:r;const s=l.value=e(...t);try{const e=await s;return l.value===s&&(a.value=e),e}catch(e){return l.value===s&&(o.value=e,a.value=null),i?s:void 0}finally{l.value===s&&(n.value=!1)}},result:a,promise:l,loading:n,error:o}}const p=Symbol(void 0),d=Symbol(void 0),g=async(e,t,r,n)=>{const u=t[p].value;let o=-1;const a=e.maxRetries||9001,l=e.retryDelay||h;t.retryErrors.value=[],t.isRetrying.value=!1,t.nextRetry.value=void 0;let c=void 0;do{let e=!1,y=null;try{if(++o,y=n?r(...n):r(),v(y)&&(y=await y),t[d].value)return null;e=!0}catch(e){y=null,t.retryErrors.value.push(e)}if(u!==t[p].value)return y;if(e)return t.isRetrying.value=!1,t.nextRetry.value=void 0,y;if(o>=a)return t.isRetrying.value=!1,t.nextRetry.value=void 0,Promise.reject(new Error(`[useRetry] max retries reached ${a}`));t.isRetrying.value=!0;const m=Date.now(),g=l(o),h=v(g)?await g:g;if(!v(g)||h){if(s(h))c=h;else{if(!i(h))throw new Error(`[useRetry] invalid value received from options.retryDelay '${typeof h}'`);c=h.getTime()}c<m?t.nextRetry.value=m+c:(t.nextRetry.value=c,c-=m),c>0&&await f(c)}if(t[d].value)return null;if(u!==t[p].value)return y}while(o<9e3);return null};const h=()=>0;function w(e,t=50,r={isImmediate:!1}){let n;return function(...u){const o=this,a=r.isImmediate&&void 0===n;void 0!==n&&clearTimeout(n),n=setTimeout((function(){n=void 0,r.isImmediate||e.apply(o,u)}),t),a&&e.apply(o,u)}}return e.debounce=w,e.exponentialDelay=e=>{const t=100*Math.pow(2,e);return t+.2*t*Math.random()},e.isArray=r,e.isBoolean=u,e.isDate=e=>o(e)&&n(e.getTime),e.isElement=e=>o(e)&&!!e.tagName,e.isFunction=n,e.isNumber=e=>"number"==typeof e,e.isObject=o,e.isPromise=function(e){return o(e)&&n(e.then)&&n(e.catch)},e.isString=e=>"string"==typeof e,e.isSymbol=e=>"symbol"==typeof e,e.minMax=function(e,t,r){return e<t?t:e>r?r:e},e.noDelay=h,e.promisedTimeout=function(e){return new Promise(t=>{setTimeout(t,e)})},e.unwrap=function(e){return t.isRef(e)?e.value:e},e.useArrayPagination=function(e,r){const n=a(e),u=y({currentPage:1,pageSize:10,...r,total:t.computed(()=>n.value.length)}),o=t.computed(()=>{const e=n.value;return Array.isArray(e)?e.slice(u.offset.value,u.offset.value+u.pageSize.value):[]});return{...u,result:o}},e.useCancellablePromise=function(e,r=!1){const n=t.ref(!1);let u=void 0;return{...m((...t)=>(e=>new Promise((t,r)=>{u=e=>{n.value=!0,r(e)},e.then(t).catch(r)}))(e(...t)),r),cancel:e=>u(e),cancelled:n}},e.useDebounce=function(e,t,r){return w(e,t,r)},e.usePagination=y,e.usePromise=m,e.useRetry=function(e,r){const n=!e||l(e)?{}:e,u=l(e)?e:r;if(!l(e)&&!c(e))throw new Error("[useRetry] options needs to be 'object'");if(u&&!l(u))throw new Error("[useRetry] factory needs to be 'function'");const o=t.ref(!1),a=t.ref(),i=t.ref([]),s={value:!1},v={isRetrying:o,retryCount:t.computed(()=>i.value.length),nextRetry:a,retryErrors:i,[p]:{value:0},[d]:s},f=u?(...e)=>(++v[p].value,g(n,v,u,e)):e=>(++v[p].value,g(n,v,e,void 0));return{...v,cancel:()=>{v.isRetrying.value=!1,v.retryErrors.value.push(new Error("[useRetry] cancelled")),v.nextRetry.value=void 0,s.value=!0},exec:f}},e.wrap=function(e){return t.isRef(e)?e:t.ref(e)},e}({},vueCompositionApi);
{
"name": "@vue-composable/core",
"version": "1.0.0-dev.2",
"version": "1.0.0-dev.3",
"description": "@vue-composable/core",

@@ -38,3 +38,3 @@ "main": "index.js",

"homepage": "https://github.com/pikax/vue-composable/tree/dev/packages/core#readme",
"peerDependencies": {
"dependencies": {
"@vue/composition-api": "^0.3.4",

@@ -41,0 +41,0 @@ "vue": "^2.6.10"

@@ -7,2 +7,5 @@ # @vue-composable/core

# vue-next
> For [vue-next](https://github.com/vuejs/vue-next) support please check [@next](https://www.npmjs.com/package/@vue-composable/core/v/next)
## Installing

@@ -9,0 +12,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc