Skip to content

Commit

Permalink
rename callback to handler
Browse files Browse the repository at this point in the history
  • Loading branch information
planttheidea committed Dec 16, 2023
1 parent 2ad411c commit 1d781b4
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 93 deletions.
198 changes: 126 additions & 72 deletions index.d.ts
Expand Up @@ -4,96 +4,150 @@ type Dictionary<Type> = {
};

/**
* iterables
* collections
*/
type ArrayIterable = any[];
type ObjectIterable = Dictionary<any>;
type ArrayCollection<Item> = Item[];
type ObjectCollection<Item> = Dictionary<Item>;

/**
* the handlers for the iterables, specific to type
* the handlers for the collections, specific to type
*/
type ArrayHandler = (value: any, index: number, array: ArrayIterable) => any;
type ObjectHandler = (value: any, key: string, object: ObjectIterable) => any;
type ArrayHandler<Item, Result> = (
value: Item,
index: number,
array: ArrayCollection,
) => Result;
type ObjectHandler<Item, Result> = (
value: Item,
key: string,
object: ObjectCollection,
) => Result;

type Every = typeof Array.prototype.findIndex;

/**
* available exports
*/

declare function every(iterable: ArrayIterable, handler: ArrayHandler): boolean;
declare function everyObject(iterable: ObjectIterable, handler: ObjectHandler): boolean;
declare function everyRight(iterable: ArrayIterable, handler: ArrayHandler): boolean;

declare function filter<Iterable = ArrayIterable>(
iterable: Iterable,
handler: ArrayHandler,
): Iterable;
declare function filterObject<Iterable = ObjectIterable>(
iterable: ObjectIterable,
handler: ObjectHandler,
): Iterable;
declare function filterRight<Iterable = ArrayIterable>(
iterable: Iterable,
handler: ArrayHandler,
): Iterable;
declare function every<Item>(
collection: ArrayCollection<Item>,
handler: ArrayHandler<Item, unknown>,
): boolean;
declare function everyObject<Item>(
collection: ObjectCollection<Item>,
handler: ObjectHandler<Item, unknown>,
): boolean;
declare function everyRight<Item>(
collection: ArrayCollection<Item>,
handler: ArrayHandler<Item, unknown>,
): boolean;

declare function find(iterable: ArrayIterable, handler: ArrayHandler): any;
declare function findObject(iterable: ObjectIterable, handler: ObjectHandler): any;
declare function findRight(iterable: ArrayIterable, handler: ArrayHandler): any;
declare function filter<Item>(
collection: ArrayCollection<Item>,
handler: ArrayHandler<Item>,
): ArrayCollection<Item, unknown>;
declare function filterObject<Item>(
collection: ObjectCollection<Item>,
handler: ObjectHandler<Item, unknown>,
): ObjectCollection<Item>;
declare function filterRight<Item>(
collection: ArrayCollection<Item>,
handler: ArrayHandler<Item, unknown>,
): ArrayCollection<Item>;

declare function findIndex<Iterable = ArrayIterable>(
iterable: Iterable,
handler: ArrayHandler,
): keyof Iterable | -1;
declare function findIndexRight<Iterable = ArrayIterable>(
iterable: Iterable,
handler: ArrayHandler,
): keyof Iterable | -1;
declare function find<Item>(
collection: ArrayCollection<Item>,
handler: ArrayHandler<Item, unknown>,
): Item | undefined;
declare function findObject<Item>(
collection: ObjectCollection<Item>,
handler: ObjectHandler<Item, unknown>,
): Item | undefined;
declare function findLast(
collection: ArrayCollection<Item>,
handler: ArrayHandler<Item, unknown>,
): Item | undefined;

declare function findKey<Iterable = ObjectIterable>(
iterable: Iterable,
handler: ObjectHandler,
): keyof Iterable | void;
declare function findIndex<Item, Collection extends ArrayCollection<Item>>(
collection: Collection,
handler: ArrayHandler<Item, unknown>,
): keyof Collection | -1;
declare function findKey<Item, Collection extends ObjectCollection<Item>>(
collection: Collection,
handler: ObjectHandler<Item, unknown>,
): keyof Collection | void;
declare function findLastIndex<Item, Collection extends ObjectCollection<Item>>(
collection: Collection,
handler: ArrayHandler<Item, unknown>,
): keyof Collection | -1;

declare function forEach(iterable: ArrayIterable, handler: ArrayHandler): void;
declare function forEachObject(iterable: ObjectIterable, handler: ObjectHandler): void;
declare function forEachRight(iterable: ArrayIterable, handler: ArrayHandler): void;
declare function forEach<Item>(
collection: ArrayCollection<Item>,
handler: ArrayHandler<Item, void>,
): void;
declare function forEachObject<Item>(
collection: ObjectCollection<Item>,
handler: ObjectHandler<Item, void>,
): void;
declare function forEachRight<Item>(
collection: ArrayCollection<Item>,
handler: ArrayHandler<Item, void>,
): void;

declare function map(iterable: ArrayIterable, handler: ArrayHandler): ArrayIterable;
declare function mapObject(iterable: ObjectIterable, handler: ObjectHandler): ObjectIterable;
declare function mapRight(iterable: ArrayIterable, handler: ArrayHandler): ArrayIterable;
declare function map<Item, Result>(
collection: ArrayCollection<Item>,
handler: ArrayHandler<Item, Result>,
): ArrayCollection<Result>;
declare function mapObject<Item, Result>(
collection: ObjectCollection<Item>,
handler: ObjectHandler<Item, Result>,
): ObjectCollection<Result>;
declare function mapRight<Item, Result>(
collection: ArrayCollection<Item>,
handler: ArrayHandler<Item, Result>,
): ArrayCollection<Result>;

type ArrayReduceHandler = (
accumulator?: any,
value?: any,
type ArrayReduceHandler<Item, Result> = (
accumulator?: Result,
value?: Item,
index?: number,
array?: ArrayIterable,
) => any;
array?: ArrayCollection,
) => Result;
type ObjectReduceHandler = (
accumulator?: any,
value?: any,
accumulator?: Result,
value?: Item,
key?: string,
object?: ObjectIterable,
) => any;
object?: ObjectCollection,
) => Result;

declare function reduce(
iterable: ArrayIterable,
handler: ArrayReduceHandler,
initialValue?: any,
): any;
declare function reduceObject(
iterable: ObjectIterable,
handler: ObjectReduceHandler,
initialValue?: any,
): any;
declare function reduceRight(
iterable: ArrayIterable,
handler: ArrayReduceHandler,
initialValue?: any,
): any;
declare function reduce<Item, Result>(
collection: ArrayCollection<Item>,
handler: ArrayReduceHandler<Item, Result>,
initialValue?: Result,
): Result;
declare function reduceObject<Item, Result>(
collection: ObjectCollection<Item>,
handler: ObjectReduceHandler<Item, Result>,
initialValue?: Result,
): Result;
declare function reduceRight<Item, Result>(
collection: ArrayCollection<Item>,
handler: ArrayReduceHandler<Item, Result>,
initialValue?: Result,
): Result;

declare function some(iterable: ArrayIterable, handler: ArrayHandler): boolean;
declare function someObject(iterable: ObjectIterable, handler: ObjectHandler): boolean;
declare function someRight(iterable: ArrayIterable, handler: ArrayHandler): boolean;
declare function some<Item>(
collection: ArrayCollection<Item>,
handler: ArrayHandler<Item, unknown>,
): boolean;
declare function someObject<Item>(
collection: ObjectCollection<Item>,
handler: ObjectHandler<Item, unknown>,
): boolean;
declare function someRight<Item>(
collection: ArrayCollection<Item>,
handler: ArrayHandler<Item, unknown>,
): boolean;

export {
every,
Expand All @@ -104,10 +158,10 @@ export {
filterRight,
find,
findIndex,
findIndexRight,
findLastIndex,
findKey,
findObject,
findRight,
findLast,
forEach,
forEachObject,
forEachRight,
Expand Down
38 changes: 19 additions & 19 deletions src/handlers.js
Expand Up @@ -47,14 +47,14 @@ function createHandlers(babel) {
};

function getInjectedBodyAndLogic({
callback,
handler,
isForEach,
isReduce,
local,
path,
statement,
}) {
const body = callback.get('body');
const body = handler.get('body');
const traverseState = {
containsThis: false,
count: 0,
Expand Down Expand Up @@ -94,7 +94,7 @@ function createHandlers(babel) {
const localFnName = path.scope.generateUidIdentifier('fn');
const localFn = templates.localVariable({
LOCAL: localFnName,
VALUE: callback.node,
VALUE: handler.node,
});

statement.insertBefore(localFn);
Expand All @@ -110,7 +110,7 @@ function createHandlers(babel) {
};
}

if (callback.isFunction()) {
if (handler.isFunction()) {
return isForEach
? { injectedBody: body.node, logic: undefined }
: { injectedBody: [], logic: body.node };
Expand All @@ -119,15 +119,15 @@ function createHandlers(babel) {
if (isForEach) {
const injectedBody = [
t.expressionStatement(
t.callExpression(callback.node, getCachedFnArgs(local, isReduce)),
t.callExpression(handler.node, getCachedFnArgs(local, isReduce)),
),
];

return { injectedBody, logic: undefined };
}

const logic = t.callExpression(
callback.node,
handler.node,
getCachedFnArgs(local, isReduce),
);

Expand All @@ -137,7 +137,7 @@ function createHandlers(babel) {
function getLocalReferences(path, statement, isReduce) {
const args = path.get('arguments');

const [collection, callback] = args;
const [collection, handler] = args;

let localCollection = collection.node;

Expand All @@ -159,11 +159,11 @@ function createHandlers(babel) {

let localDestructuredRefName;

if (callback.isFunction()) {
if (handler.isFunction()) {
if (isReduce) {
[accumulated, value, key, scopedCollection] = callback.get('params');
[accumulated, value, key, scopedCollection] = handler.get('params');
} else {
[value, key, scopedCollection] = callback.get('params');
[value, key, scopedCollection] = handler.get('params');
}

if (value && (value.isArrayPattern() || value.isObjectPattern())) {
Expand All @@ -175,7 +175,7 @@ function createHandlers(babel) {
VALUE: localDestructuredRefName,
});

const body = callback.get('body');
const body = handler.get('body');

if (!body.isBlockStatement()) {
body.replaceWith(t.blockStatement([t.returnStatement(body.node)]));
Expand Down Expand Up @@ -236,15 +236,15 @@ function createHandlers(babel) {
handleInvalidUsage({ handlers, path });
handleArrowFunctionExpressionUse(path);

const [collection, callback] = path.get('arguments');
const [collection, handler] = path.get('arguments');

processNestedInlineLoopMacros(collection, handlers);

const statement = path.getStatementParent();
const local = getLocalReferences(path, statement);

const { injectedBody, logic } = getInjectedBodyAndLogic({
callback,
handler,
local,
path,
statement,
Expand Down Expand Up @@ -351,15 +351,15 @@ function createHandlers(babel) {
handleInvalidUsage({ handlers, path });
handleArrowFunctionExpressionUse(path);

const [collection, callback] = path.get('arguments');
const [collection, handler] = path.get('arguments');

processNestedInlineLoopMacros(collection, handlers);

const statement = path.getStatementParent();
const local = getLocalReferences(path, statement);

const { injectedBody, logic } = getInjectedBodyAndLogic({
callback,
handler,
local,
path,
statement,
Expand Down Expand Up @@ -466,7 +466,7 @@ function createHandlers(babel) {
handleInvalidUsage({ handlers, path });
handleArrowFunctionExpressionUse(path);

const [collection, callback] = path.get('arguments');
const [collection, handler] = path.get('arguments');

processNestedInlineLoopMacros(collection, handlers);

Expand All @@ -477,7 +477,7 @@ function createHandlers(babel) {
const result = path.scope.generateUidIdentifier('result');

const { injectedBody, logic } = getInjectedBodyAndLogic({
callback,
handler,
isForEach,
local,
path,
Expand Down Expand Up @@ -634,15 +634,15 @@ function createHandlers(babel) {
handleInvalidUsage({ handlers, path });
handleArrowFunctionExpressionUse(path);

const [collection, callback, initialValue] = path.get('arguments');
const [collection, handler, initialValue] = path.get('arguments');

processNestedInlineLoopMacros(collection, handlers);

const statement = path.getStatementParent();
const local = getLocalReferences(path, statement, true);

const { injectedBody, logic } = getInjectedBodyAndLogic({
callback,
handler,
isReduce: true,
local,
path,
Expand Down

0 comments on commit 1d781b4

Please sign in to comment.