@-0/utils
Advanced tools
Comparing version 0.2.109 to 0.2.110
@@ -1,1 +0,1 @@ | ||
export declare function get_param_names(func: any, CMD?: any): any[]; | ||
export declare function get_shallow_static_destructured_params(func: any, CMD?: any): any[]; |
@@ -5,3 +5,3 @@ import { CMD_WORK } from "@-0/keys"; | ||
? ` | ||
🔥 Warning: \`registerCMD\` 🔥 | ||
Warning: \`registerCMD\` \`${CMD_WORK}\` handler analysis | ||
@@ -17,3 +17,3 @@ Computed property names prevent static analysis of args | ||
: ` | ||
🔥 Warning: Complex Properties 🔥 | ||
Warning: Complex Properties | ||
@@ -28,5 +28,6 @@ ${name}({ ${comp} ... | ||
const ARGUMENT_NAMES = /([^\s,]+)/g; | ||
const DESTRUCT = /{|}/g; | ||
const DST_BEG = /{/g; | ||
const DST_END = /}/g; | ||
const VALID_VAR = /^[a-zA-Z_$][a-zA-Z_$0-9]*$/g; | ||
export function get_param_names(func, CMD = null) { | ||
export function get_shallow_static_destructured_params(func, CMD = null) { | ||
const fnStr = func.toString().replace(STRIP_COMMENTS, ""); | ||
@@ -38,7 +39,15 @@ let results = fnStr.slice(fnStr.indexOf("(") + 1, fnStr.indexOf(")")).match(ARGUMENT_NAMES); | ||
return []; | ||
let is_destructured = false; | ||
const computed = results.map(x => x.replace(DESTRUCT, "")).filter(x => x !== ""); | ||
let idx_map = {}; | ||
results.forEach((c, i) => { | ||
if (c.match(DST_BEG) && !idx_map["start"]) | ||
return (idx_map["start"] = i); | ||
if (c.match(DST_END)) | ||
return (idx_map["end"] = i); | ||
}); | ||
if (!idx_map["end"]) | ||
return []; | ||
const destructured = results.slice(idx_map["start"] + 1, idx_map["end"]); | ||
let done = []; | ||
let warned = false; | ||
computed.forEach((c, i, d) => { | ||
destructured.forEach((c, i, d) => { | ||
if (c.match(/\]/g)) { | ||
@@ -45,0 +54,0 @@ d.splice(i + 1, 1); |
@@ -8,2 +8,2 @@ export { msTaskPromiseDelay } from "./taskDelay"; | ||
export { Err_missing_props } from "./Error_strings"; | ||
export { get_param_names } from "./get_param_names"; | ||
export { get_shallow_static_destructured_params } from "./get_param_names"; |
@@ -8,2 +8,2 @@ export { msTaskPromiseDelay } from "./taskDelay"; | ||
export { Err_missing_props } from "./Error_strings"; | ||
export { get_param_names } from "./get_param_names"; | ||
export { get_shallow_static_destructured_params } from "./get_param_names"; |
@@ -5,3 +5,3 @@ { | ||
"license": "MIT", | ||
"version": "0.2.109", | ||
"version": "0.2.110", | ||
"description": "utilities for the `-0` org/framework built on @thi.ng/umbrella ecosystem", | ||
@@ -31,6 +31,6 @@ "main": "./lib/index.js", | ||
"dependencies": { | ||
"querystring": "^0.2.0" | ||
"querystring": "^0.2.1" | ||
}, | ||
"peerDependencies": { | ||
"@-0/keys": "^0.2.112", | ||
"@-0/keys": "^0.2.113", | ||
"@thi.ng/checks": "^2.8.0", | ||
@@ -40,3 +40,3 @@ "@thi.ng/rstream": "^5.1.0" | ||
"devDependencies": { | ||
"@-0/keys": "^0.2.112", | ||
"@-0/keys": "^0.2.113", | ||
"@babel/plugin-transform-modules-commonjs": "^7.12.13", | ||
@@ -47,4 +47,4 @@ "@thi.ng/checks": "^2.8.0", | ||
"@types/node": "^14.14.28", | ||
"@typescript-eslint/eslint-plugin": "^4.15.0", | ||
"@typescript-eslint/parser": "^4.15.0", | ||
"@typescript-eslint/eslint-plugin": "^4.15.1", | ||
"@typescript-eslint/parser": "^4.15.1", | ||
"babel-jest": "^26.6.3", | ||
@@ -51,0 +51,0 @@ "eslint": "^7.20.0", |
import { get_param_names } from "../lib" | ||
/** | ||
* @module commands/register | ||
*/ | ||
import { map } from "@thi.ng/transducers" | ||
import { isFunction } from "@thi.ng/checks" | ||
import { stream, pubsub } from "@thi.ng/rstream" | ||
import { CMD_SUB$, CMD_ARGS, CMD_RESO, CMD_ERRO, CMD_SRC$, CMD_WORK } from "@-0/keys" | ||
import { xKeyError, diff_keys, stringify_fn } from "@-0/utils" | ||
const err_str = "command Registration `registerCMD`" | ||
const no_work_or_src_error = ` | ||
Error registering ${CMD_SUB$}: | ||
Commands with no \`${CMD_WORK}\` & no \`${CMD_SRC$}\` handler | ||
can/need not be registered: | ||
- \`${CMD_WORK}\`: registers side-effecting handlers | ||
- \`${CMD_SRC$}\`: registers upstream Command producers | ||
if your Command is for data acquisition/transformation, | ||
you can run$.next(YOUR_COMMAND) without registration. | ||
` | ||
const warnOnIncongruentInput = (work_params, sub$) => (args, CMD) => { | ||
const args_params = Object.keys(args) | ||
let missing = work_params.reduce((a, c) => (args_params.some(x => x === c) ? a : a.concat(c)), []) | ||
if (!missing.length) return | ||
console.warn( | ||
`Command { \`${CMD_SUB$}\`: '${sub$}' } missing argument${missing.length === 1 | ||
? "" | ||
: "s"} specified by its \`${CMD_WORK}\` handler: ${missing.map(x => `\`${x}\``)} | ||
${stringify_fn(CMD, 2)} | ||
` | ||
) | ||
// return args_params | ||
} | ||
const test$ = pubsub({ | ||
topic : x => x[CMD_SUB$], | ||
id : "out$_stream" | ||
//equiv: (res, tpc) => res === tpc | ||
}) | ||
export const registerCMD = (command = null) => { | ||
const sub$ = command[CMD_SUB$] | ||
const args = command[CMD_ARGS] | ||
const erro = command[CMD_ERRO] | ||
const reso = command[CMD_RESO] | ||
const src$ = command[CMD_SRC$] | ||
const work = command[CMD_WORK] | ||
if (!work && !src$) { | ||
throw new Error(no_work_or_src_error) | ||
} | ||
const known_CMD_props = [ CMD_SUB$, CMD_ARGS, CMD_RESO, CMD_ERRO, CMD_SRC$, CMD_WORK ] | ||
const [ unknown_CMD_props ] = diff_keys(known_CMD_props, command) | ||
// console.log({ known_CMD_props, unknown_CMD_props }) | ||
if (unknown_CMD_props.length > 0) { | ||
throw new Error(xKeyError(err_str, command, unknown_CMD_props, undefined)) | ||
} | ||
const sans_src = { ...command, [CMD_SRC$]: undefined } | ||
const work_params = get_param_names(work, sans_src) | ||
const param_warning = work_params.length ? warnOnIncongruentInput(work_params, sub$) : false | ||
const CMD = reso | ||
? { | ||
[CMD_SUB$] : sub$, | ||
[CMD_ARGS] : args, | ||
[CMD_RESO] : reso, | ||
[CMD_ERRO] : erro | ||
} | ||
: { [CMD_SUB$]: sub$, [CMD_ARGS]: args } | ||
// @ts-ignore | ||
test$.subscribeTopic( | ||
sub$, | ||
{ | ||
next : x => { | ||
param_warning && param_warning(x[CMD_ARGS], x) | ||
//log$.next(x) // send every Command to log$ stream | ||
return work(x[CMD_ARGS]) // execute side-effects, etc. | ||
}, | ||
error : console.warn | ||
} // pluck the args from the incoming Command | ||
//map(x => x[CMD_ARGS]) | ||
) | ||
return CMD | ||
} | ||
const _1 = function({ d, e, s }) {} | ||
@@ -9,3 +105,3 @@ const _2 = ({ d, e, s }) => {} | ||
const _6 = ({ ["a" + "b"]: c, d }, x) => {} | ||
const _7 = ({ [`comp `]: c, d }) => {} | ||
const _7 = ({ [`comp`]: c, d }) => {} | ||
@@ -42,16 +138,31 @@ // prettier-ignore | ||
}, | ||
[`another` + 1]: s_c = `destructured`, | ||
[`another` + 1]: s_c = `destruct`, | ||
t | ||
}) => {} | ||
get_param_names(_1) //? | ||
get_param_names(_2) //? | ||
get_param_names(_3) //? | ||
get_param_names(_4) //? | ||
get_param_names(_5) //? | ||
get_param_names(_6) //? | ||
get_param_names(_7) //? | ||
get_param_names(_8) //? | ||
get_param_names(_9) //? | ||
get_param_names(_10) //? | ||
get_param_names(_11) //? | ||
//get_param_names(_1) //? | ||
//get_param_names(_2) //? | ||
//get_param_names(_3) //? | ||
//get_param_names(_4) //? | ||
//get_param_names(_5) //? | ||
//get_param_names(_6) //? | ||
//get_param_names(_7) //? | ||
//get_param_names(_8) //? | ||
//get_param_names(_9) //? | ||
//get_param_names(_10) //? | ||
//get_param_names(_11) //? | ||
const cb = ({ fire, ["computed"]: prop }) => ({ rocket: fire + "🚀" }) | ||
const TEST2 = registerCMD({ | ||
[CMD_SUB$] : "TEST2", | ||
[CMD_ARGS] : { fire: "🍑" }, | ||
[CMD_WORK] : _11 | ||
}) //? | ||
test$.next(TEST2) | ||
const TEST3 = registerCMD({ | ||
[CMD_SUB$] : "TEST3", | ||
[CMD_ARGS] : { fire: "🎹" }, | ||
[CMD_WORK] : _7 | ||
}) //? |
@@ -1,2 +0,2 @@ | ||
import { get_param_names } from "../src" | ||
import { get_shallow_static_destructured_params } from "../src" | ||
@@ -8,15 +8,17 @@ const warned = (x = jest.fn()) => (jest.spyOn(console, "warn").mockImplementation(x), x) | ||
const _1 = function({ d, e, s }) {} | ||
expect(get_param_names(_1)).toMatchObject([ "d", "e", "s" ]) | ||
expect(get_shallow_static_destructured_params(_1)).toMatchObject([ "d", "e", "s" ]) | ||
}) | ||
test("2: single destructured arrow function object", () => { | ||
const _2 = ({ d, e, s }) => {} | ||
expect(get_param_names(_2)).toMatchObject([ "d", "e", "s" ]) | ||
expect(get_shallow_static_destructured_params(_2)).toMatchObject([ "d", "e", "s" ]) | ||
}) | ||
test("3: one destructured arg and one positional (arrow)", () => { | ||
const _3 = ({ d, e, s }, x) => {} | ||
expect(get_param_names(_3)).toMatchObject([ "d", "e", "s", "x" ]) | ||
expect(get_shallow_static_destructured_params(_3)).toMatchObject([ "d", "e", "s" ]) | ||
}) | ||
test("4: two positional args", () => { | ||
const _4 = function(x, y) {} | ||
expect(get_param_names(_4)).toMatchObject([ "x", "y" ]) | ||
expect(get_shallow_static_destructured_params(_4)).toMatchObject([]) | ||
}) | ||
@@ -26,10 +28,10 @@ test("5: one destructured arg and one positional (`function`)", () => { | ||
const _5 = function({ d, e, s }, x) {} | ||
expect(get_param_names(_5)).toMatchObject([ "d", "e", "s", "x" ]) | ||
expect(get_shallow_static_destructured_params(_5)).toMatchObject([ "d", "e", "s" ]) | ||
expect(warn_spy.mock.calls.length).toBe(0) | ||
}) | ||
test("6: Computed Property and positional", () => { | ||
//const warn_spy = warned() | ||
const warn_spy = warned() | ||
const _6 = ({ ["a" + "b"]: c, d }, x) => {} | ||
expect(get_param_names(_6)).toMatchObject([ "d", "x" ]) | ||
//expect(warn_spy.mock.calls.length).toBe(1) | ||
expect(get_shallow_static_destructured_params(_6)).toMatchObject([ "d" ]) | ||
expect(warn_spy.mock.calls.length).toBe(1) | ||
}) | ||
@@ -39,3 +41,3 @@ test("7: Computed Property with space", () => { | ||
const _7 = ({ [`comp `]: c, d }) => {} | ||
expect(get_param_names(_7)).toMatchObject([ "d" ]) | ||
expect(get_shallow_static_destructured_params(_7)).toMatchObject([ "d" ]) | ||
expect(warn_spy.mock.calls.length).toBe(1) | ||
@@ -45,17 +47,17 @@ }) | ||
// prettier-ignore | ||
const _8 = ({ | ||
d, | ||
const _8 = ({ | ||
d, | ||
e, | ||
s | ||
s | ||
}) => {} | ||
expect(get_param_names(_8)).toMatchObject([ "d", "e", "s" ]) | ||
expect(get_shallow_static_destructured_params(_8)).toMatchObject([ "d", "e", "s" ]) | ||
}) | ||
test("9: destructured line breaks with inline comments", () => { | ||
// prettier-ignore | ||
const _9 = ({ | ||
d, // with | ||
const _9 = ({ | ||
d, // with | ||
e, // inline | ||
s // comments | ||
}) => {} | ||
expect(get_param_names(_9)).toMatchObject([ "d", "e", "s" ]) | ||
expect(get_shallow_static_destructured_params(_9)).toMatchObject([ "d", "e", "s" ]) | ||
}) | ||
@@ -66,7 +68,7 @@ test("10: positional line breaks with comments", () => { | ||
// with comment above | ||
d, | ||
e, | ||
s | ||
d, | ||
e, | ||
s | ||
) => {} | ||
expect(get_param_names(_10)).toMatchObject([ "d", "e", "s" ]) | ||
expect(get_shallow_static_destructured_params(_10)).toMatchObject([]) | ||
}) | ||
@@ -77,5 +79,5 @@ test("11: Computed properties &+ default values: total warnings = 1", () => { | ||
// prettier-ignore | ||
const _11 = ({ | ||
const _11 = ({ | ||
// with comment above | ||
[`comp `]: c, // inline | ||
[ `comp `]: c, // inline | ||
d, | ||
@@ -85,3 +87,3 @@ [`another` + "comp"]: w = "destructured", | ||
}) => {} | ||
expect(get_param_names(_11)).toMatchObject([ "d", "e" ]) | ||
expect(get_shallow_static_destructured_params(_11)).toMatchObject([ "d", "e" ]) | ||
expect(warn_spy.mock.calls.length).toBe(1) | ||
@@ -93,3 +95,3 @@ }) | ||
// prettier-ignore | ||
const _11 = ({ | ||
const _11 = ({ | ||
// with comment above | ||
@@ -101,3 +103,3 @@ c: { | ||
}) => {} | ||
expect(get_param_names(_11)).toMatchObject([ "d", "e" ]) | ||
expect(get_shallow_static_destructured_params(_11)).toMatchObject([ "d", "e" ]) | ||
expect(warn_spy.mock.calls.length).toBe(1) | ||
@@ -108,6 +110,6 @@ }) | ||
// prettier-ignore | ||
expect(get_param_names((x) => x)).toMatchObject([]) | ||
expect(get_param_names(x => x)).toMatchObject([]) | ||
expect(get_shallow_static_destructured_params((x) => x)).toMatchObject([]) | ||
expect(get_shallow_static_destructured_params(x => x)).toMatchObject([]) | ||
expect(warn_spy.mock.calls.length).toBe(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
31861
711
Updatedquerystring@^0.2.1