Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

export internal API for user customisations #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import {
OutputSelector,
OutputParametricSelector
} from "reselect";
import {
Iterable,
List
} from "immutable";

Comment on lines +7 to +11
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this create a hard dependency on immutable?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, I think it does - is that a problem?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@heyimalex hi, just a friendly reminder ;) would you like me to remove that "hard dependency"?


export as namespace ReselectMap;

Expand All @@ -20,6 +25,33 @@ export const objectMemoize: MemoizeFunc;
export const listMemoize: MemoizeFunc;
export const mapMemoize: MemoizeFunc;

export function createMappedSelectorCreator(
memoizeFunc: MemoizeFunc,
equalityCheck?: <T>(a: T, b: T) => boolean
): typeof createArraySelector;

type ArrayMapper<V> = (arr: Array<V>, callback: (value: V) => V) => Array<V>;
type ImmutableListMapper<V> = (list: List<V>, callback: (value: V) => V) => List<V>;

export type MemoizeListOptions<V> = {
mapper: ArrayMapper<V> | ImmutableListMapper<V>;
equalityCheck: <T>(a: T, b: T) => boolean;
unique?: boolean;
};
export function memoizeList<F extends Function, V>(fn: F, memoizeOptions: MemoizeListOptions<V>): F;

type ArrayWithIndexMapper<V> = (arr: Array<V>, callback: (key: number, value: V) => V) => Array<V>;
type ObjectMapper<K extends keyof any, V> = (record: Record<K, V>, callback: (key: K, value: V) => V) => Record<K, V>;
type ImmutableIterableMapper<K, V> = (arr: Iterable<K, V>, callback: (key: K, value: V) => V) => Iterable<K, V>;

export type MemoizeMapOptions<K extends keyof any, V> = {
mapper: ArrayWithIndexMapper<V> | ObjectMapper<K, V> | ImmutableIterableMapper<K, V>;
equalityCheck: <T>(a: T, b: T) => boolean;
unique?: boolean | true;
};

export function memoizeMap<F extends Function, K extends keyof any, V>(fn: F, memoizeOptions: MemoizeMapOptions<K, V>): F;

export function createArraySelectorCreator(
equalityCheck: <T>(a: T, b: T) => boolean
): typeof createArraySelector;
Expand Down
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const mapMemoize = (fn, equalityCheck) =>
// wrapping the result func and defaultMemoize for wrapping the selector. It
// sucks that we're relying on an implementation detail but I'll be back to fix
// this code again whenever it breaks :)
function createMappedSelectorCreator(memoize, ...memoizeOptions) {
export function createMappedSelectorCreator(memoize, ...memoizeOptions) {
return createSelectorCreator((fn, mapmem) => {
if (mapmem === true) {
return memoize(fn, ...memoizeOptions);
Expand All @@ -80,3 +80,5 @@ export const createListSelectorCreator = equalityCheck =>
createMappedSelectorCreator(listMemoize, equalityCheck);
export const createMapSelectorCreator = equalityCheck =>
createMappedSelectorCreator(mapMemoize, equalityCheck);

export { memoizeMap, memoizeList };
18 changes: 17 additions & 1 deletion typescript_test/test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {
createArraySelector,
createObjectSelector,
createArraySelectorCreator
createArraySelectorCreator,
createMappedSelectorCreator,
memoizeMap
} from "../src/index";

function testArraySelector() {
Expand Down Expand Up @@ -75,3 +77,17 @@ function testArraySelectorCreator() {

const foo: number[] = selector({ items: [1, 2, 3], mul: 5 });
}

const arrayWithIndexMemoize = <F extends Function, T> (fn: F, equalityCheck: <T>(a: T, b: T) => boolean) =>
memoizeMap(fn, {
equalityCheck,
mapper: (arr: Array<T>, callback: (key: number, value: T) => T) => {
const result = [];
for (let i = 0; i < arr.length; i++) {
result[i] = callback(i, arr[i]);
}
return result;
}
});

createMappedSelectorCreator(arrayWithIndexMemoize);