react-hooks-library
Advanced tools
Comparing version 0.0.13 to 0.0.14
import { IStateKey, IStateStore, IStateSetter } from './useParams'; | ||
declare type IEqual = ((filterState: any, dataValue: any) => boolean) | undefined; | ||
interface IFilters { | ||
[key: string]: { | ||
equals: ((filterState: any, dataValue: any) => boolean) | undefined; | ||
equals: IEqual; | ||
}; | ||
} | ||
declare type Itreater = (data: any[], filterStore: IStateStore) => any[]; | ||
export declare function useFilter(data: any[], keys: IStateKey, filters: IFilters, treater?: Itreater): [any, IStateStore, IStateSetter]; | ||
declare type Itreater<P> = (data: P[], filterStore: IStateStore) => P[]; | ||
export declare function useFilter<P>(data: P[], keys?: IStateKey, filters?: IFilters, treater?: Itreater<P>): [P[], IStateStore, IStateSetter]; | ||
export {}; |
@@ -6,13 +6,16 @@ import { useParams } from './useParams'; | ||
export function useFilter(data, keys, filters, treater) { | ||
if (keys === void 0) { keys = {}; } | ||
if (treater === void 0) { treater = function (income) { return income; }; } | ||
var _a = useParams(keys), filterStore = _a[0], setFilterStoreByKey = _a[1]; | ||
return [ | ||
treater(data.filter(function (item) { | ||
return Object.keys(filters).every(function (key) { | ||
var comparer = filters[key].equals | ||
? filters[key].equals | ||
: defaultComparer; | ||
return comparer(filterStore[key], item[key]); | ||
}); | ||
}), filterStore), | ||
treater(filters | ||
? data.filter(function (item) { | ||
return Object.keys(filters).every(function (key) { | ||
var comparer = filters[key].equals | ||
? filters[key].equals | ||
: defaultComparer; | ||
return comparer(filterStore[key], item[key]); | ||
}); | ||
}) | ||
: data, filterStore), | ||
filterStore, | ||
@@ -19,0 +22,0 @@ setFilterStoreByKey, |
{ | ||
"name": "react-hooks-library", | ||
"version": "0.0.13", | ||
"version": "0.0.14", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "scripts": { |
import { IStateKey, IStateStore, IStateSetter, useParams } from './useParams' | ||
type IEqual = ((filterState: any, dataValue: any) => boolean) | undefined | ||
interface IFilters { | ||
[key: string]: { | ||
equals: ((filterState: any, dataValue: any) => boolean) | undefined | ||
equals: IEqual | ||
} | ||
} | ||
type Itreater = (data: any[], filterStore: IStateStore) => any[] | ||
type Itreater<P> = (data: P[], filterStore: IStateStore) => P[] | ||
const defaultComparer = (filterState: any, dataValue: any) => | ||
const defaultComparer: IEqual = (filterState, dataValue) => | ||
filterState === dataValue | ||
export function useFilter( | ||
data: any[], | ||
keys: IStateKey, | ||
filters: IFilters, | ||
treater: Itreater = (income: any[]) => income | ||
): [any, IStateStore, IStateSetter] { | ||
export function useFilter<P>( | ||
data: P[], | ||
keys: IStateKey = {}, | ||
filters?: IFilters, | ||
treater: Itreater<P> = (income: P[]) => income | ||
): [P[], IStateStore, IStateSetter] { | ||
const [filterStore, setFilterStoreByKey] = useParams(keys) | ||
@@ -24,11 +26,13 @@ | ||
treater( | ||
data.filter(item => | ||
Object.keys(filters).every(key => { | ||
const comparer = filters[key].equals | ||
? filters[key].equals! | ||
: defaultComparer | ||
filters | ||
? data.filter(item => | ||
Object.keys(filters).every(key => { | ||
const comparer = filters[key].equals | ||
? filters[key].equals | ||
: defaultComparer | ||
return comparer(filterStore[key], item[key]) | ||
}) | ||
), | ||
return comparer!(filterStore[key], item[key]) | ||
}) | ||
) | ||
: data, | ||
filterStore | ||
@@ -35,0 +39,0 @@ ), |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
302722
1997