-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: removes parse util in favor of added combine task and recreate …
…util
- Loading branch information
Showing
11 changed files
with
166 additions
and
116 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { Task } from '../definitions'; | ||
import { series } from '../tasks/aggregate/series'; | ||
import { stringifyRoute } from './stringify-route'; | ||
import { Members } from 'type-core'; | ||
|
||
interface Item { | ||
name: string; | ||
route: string[]; | ||
task: Task; | ||
} | ||
|
||
interface Options { | ||
include: string[] | null; | ||
exclude: string[] | null; | ||
} | ||
|
||
export function parseToRecord( | ||
options: Options, | ||
record: Task.Record | ||
): Members<Task> { | ||
const { include, exclude } = options; | ||
const arr = parseToArray(record); | ||
|
||
const members: Members<Task> = {}; | ||
for (const item of arr) { | ||
if (exclude && exclude.includes(item.name)) continue; | ||
if (include && !include.includes(item.name)) continue; | ||
members[item.name] = item.task; | ||
} | ||
|
||
if (include) { | ||
for (const name of include) { | ||
if (!Object.hasOwnProperty.call(members, name)) { | ||
throw Error(`Task not found: ${name}`); | ||
} | ||
} | ||
} | ||
|
||
return members; | ||
} | ||
|
||
export function parseToArray(record: Task.Record): Item[] { | ||
const names: string[] = []; | ||
|
||
return parseHelper(record) | ||
.map(([route, task]) => { | ||
const name = stringifyRoute(route); | ||
|
||
if (names.includes(name)) { | ||
throw Error(`Name collusion on parse: ${name}`); | ||
} | ||
|
||
names.push(name); | ||
return { name, route, task }; | ||
}) | ||
.filter((item): item is Item => Boolean(item.task)); | ||
} | ||
|
||
function parseHelper(record: Task.Record): Array<[string[], Task]> { | ||
const arr: Array<[string[], Task]> = []; | ||
|
||
for (const [name, tasks] of Object.entries(record)) { | ||
if (typeof tasks === 'function') { | ||
arr.push([[name], tasks]); | ||
} else { | ||
const all: Task[] = []; | ||
const every: Array<[string[], Task]> = []; | ||
for (const [route, task] of parseHelper(tasks)) { | ||
every.push([[name, ...route], task]); | ||
if (route.length <= 1) all.push(task); | ||
} | ||
|
||
if (all.length) arr.push([[name], series(...all)]); | ||
if (every.length) arr.push(...every); | ||
} | ||
} | ||
|
||
return arr; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import ObjectPath from 'objectpath'; | ||
|
||
export function stringifyRoute(route: Array<string | number>): string { | ||
return ObjectPath.stringify( | ||
route.map((x) => String(x)), | ||
"'", | ||
false | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Task, Context } from '../../definitions'; | ||
import { parseToRecord } from '../../helpers/parse'; | ||
import { run } from '../../utils/run'; | ||
import { recreate } from '../../utils/recreate'; | ||
import { context } from '../transform/context'; | ||
import { series } from './series'; | ||
import { into } from 'pipettes'; | ||
|
||
export function combine( | ||
tasks: Task.Record, | ||
names: string | string[] | ||
): Task.Async { | ||
const keys = Array.isArray(names) ? names : [names]; | ||
|
||
return async (ctx: Context): Promise<void> => { | ||
return into( | ||
tasks, | ||
recreate.bind(null, (task, route) => { | ||
return context( | ||
(ctx) => ({ | ||
...ctx, | ||
route: ctx.route.concat(route) | ||
}), | ||
task | ||
); | ||
}), | ||
parseToRecord.bind(null, { include: keys, exclude: null }), | ||
(record) => keys.map((key) => record[key]), | ||
(arr) => series(...arr), | ||
(task) => run(task, ctx) | ||
); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './combine'; | ||
export * from './parallel'; | ||
export * from './select'; | ||
export * from './series'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export * from './fetch'; | ||
export * from './is-cancelled'; | ||
export * from './parse'; | ||
export * from './recreate'; | ||
export * from './run'; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Task } from '../definitions'; | ||
import { Empty } from 'type-core'; | ||
|
||
export function recreate( | ||
map: (task: Task, route: string[]) => Task | Empty, | ||
tasks: Task.Record | ||
): Task.Record { | ||
return recreateHelper([], tasks, map); | ||
} | ||
|
||
function recreateHelper( | ||
route: string[], | ||
tasks: Task.Record, | ||
map: (task: Task, route: string[]) => Task | Empty | ||
): Task.Record { | ||
return Object.entries(tasks).reduce((acc, [key, value]) => { | ||
if (typeof value === 'function') { | ||
const task = map(value, route.concat(key)); | ||
return task ? { ...acc, [key]: task } : acc; | ||
} else { | ||
const record = recreateHelper(route.concat(key), value, map); | ||
return Object.keys(record).length ? { ...acc, [key]: record } : acc; | ||
} | ||
}, {} as Task.Record); | ||
} |