Comparing version
@@ -1,5 +0,10 @@ | ||
const argsOf = (x) => { | ||
function argsOf(fn) { | ||
const source = fn.toString(); | ||
let x = source; | ||
const d = x.indexOf('{') + 1; | ||
if (!d || x.slice(0, d).includes(')')) | ||
return []; | ||
return { | ||
keys: [], | ||
source | ||
}; | ||
x = x.slice(d); | ||
@@ -14,4 +19,9 @@ let match; | ||
} while (match); | ||
return x.split('}')[0].split(',').map(x => x.split(/[^\w\s$]+/g)[0].trim()); | ||
}; | ||
return { | ||
keys: x.split('}')[0] | ||
.split(',') | ||
.map((x) => x.split(/[^\w\s$]+/g)[0].trim()), | ||
source | ||
}; | ||
} | ||
/** | ||
@@ -29,5 +39,9 @@ * Extract destructured argument names from `fn`'s first argument and return a `Set` of `keys`. | ||
*/ | ||
const argtor = (fn, keys = new Set()) => (argsOf('' + fn).forEach(x => !x || keys.add(x)), keys); | ||
function argtor(fn, keys = new Set()) { | ||
const args = argsOf(fn?.fn ?? fn); | ||
args.keys.forEach(x => !x || keys.add(x)); | ||
return Object.assign([...keys], { source: args.source }); | ||
} | ||
export { argtor }; | ||
//# sourceMappingURL=argtor.js.map |
@@ -1,2 +0,2 @@ | ||
const e=(e,t=new Set)=>((e=>{const t=e.indexOf("{")+1;if(!t||e.slice(0,t).includes(")"))return[];let i;e=e.slice(t);do{i=!1,e=e.replace(/`((\\`)?[^`])+?`|'((\\')?[^'])+?'|"((\\")?[^"])+?"|\([^{[(]*?\)|\{[^{[(]*?\}|\[[^{[(]*?\]/g,()=>(i=!0,""))}while(i);return e.split("}")[0].split(",").map(e=>e.split(/[^\w\s$]+/g)[0].trim())})(""+e).forEach(e=>!e||t.add(e)),t);export{e as argtor}; | ||
function e(e,s=new Set){const t=function(e){const s=e.toString();let t=s;const n=t.indexOf("{")+1;if(!n||t.slice(0,n).includes(")"))return{keys:[],source:s};let c;t=t.slice(n);do{c=!1,t=t.replace(/`((\\`)?[^`])+?`|'((\\')?[^'])+?'|"((\\")?[^"])+?"|\([^{[(]*?\)|\{[^{[(]*?\}|\[[^{[(]*?\]/g,()=>(c=!0,""))}while(c);return{keys:t.split("}")[0].split(",").map(e=>e.split(/[^\w\s$]+/g)[0].trim()),source:s}}(e?.fn??e);return t.keys.forEach(e=>!e||s.add(e)),Object.assign([...s],{source:t.source})}export{e as argtor}; | ||
//# sourceMappingURL=argtor.min.js.map |
@@ -9,5 +9,10 @@ "use strict"; | ||
}); | ||
const argsOf = (x)=>{ | ||
function argsOf(fn) { | ||
const source = fn.toString(); | ||
let x = source; | ||
const d = x.indexOf('{') + 1; | ||
if (!d || x.slice(0, d).includes(')')) return []; | ||
if (!d || x.slice(0, d).includes(')')) return { | ||
keys: [], | ||
source | ||
}; | ||
x = x.slice(d); | ||
@@ -22,6 +27,17 @@ let match; | ||
}while (match) | ||
return x.split('}')[0].split(',').map((x)=>x.split(/[^\w\s$]+/g)[0].trim()); | ||
}; | ||
const argtor = (fn, keys = new Set())=>(argsOf('' + fn).forEach((x)=>!x || keys.add(x)), keys); | ||
return { | ||
keys: x.split('}')[0].split(',').map((x)=>x.split(/[^\w\s$]+/g)[0].trim()), | ||
source | ||
}; | ||
} | ||
function argtor(fn, keys = new Set()) { | ||
const args = argsOf(fn?.fn ?? fn); | ||
args.keys.forEach((x)=>!x || keys.add(x)); | ||
return Object.assign([ | ||
...keys | ||
], { | ||
source: args.source | ||
}); | ||
} | ||
//# sourceMappingURL=argtor.js.map |
@@ -1,4 +0,9 @@ | ||
const argsOf = (x)=>{ | ||
function argsOf(fn) { | ||
const source = fn.toString(); | ||
let x = source; | ||
const d = x.indexOf('{') + 1; | ||
if (!d || x.slice(0, d).includes(')')) return []; | ||
if (!d || x.slice(0, d).includes(')')) return { | ||
keys: [], | ||
source | ||
}; | ||
x = x.slice(d); | ||
@@ -13,4 +18,7 @@ let match; | ||
}while (match) | ||
return x.split('}')[0].split(',').map((x)=>x.split(/[^\w\s$]+/g)[0].trim()); | ||
}; | ||
return { | ||
keys: x.split('}')[0].split(',').map((x)=>x.split(/[^\w\s$]+/g)[0].trim()), | ||
source | ||
}; | ||
} | ||
/** | ||
@@ -27,4 +35,12 @@ * Extract destructured argument names from `fn`'s first argument and return a `Set` of `keys`. | ||
* @returns The keys `Set` with the argument names. | ||
*/ export const argtor = (fn, keys = new Set())=>(argsOf('' + fn).forEach((x)=>!x || keys.add(x)), keys); | ||
*/ export function argtor(fn, keys = new Set()) { | ||
const args = argsOf(fn?.fn ?? fn); | ||
args.keys.forEach((x)=>!x || keys.add(x)); | ||
return Object.assign([ | ||
...keys | ||
], { | ||
source: args.source | ||
}); | ||
} | ||
//# sourceMappingURL=argtor.js.map |
@@ -0,1 +1,2 @@ | ||
declare type Fn = (state: any) => any; | ||
/** | ||
@@ -13,2 +14,7 @@ * Extract destructured argument names from `fn`'s first argument and return a `Set` of `keys`. | ||
*/ | ||
export declare const argtor: (fn: (state: any) => any, keys?: Set<string>) => Set<string>; | ||
export declare function argtor<T extends string>(fn: Fn & { | ||
fn?: Fn; | ||
}, keys?: Set<T>): T[] & { | ||
source: string; | ||
}; | ||
export {}; |
@@ -6,3 +6,3 @@ { | ||
"description": "Extracts destructured argument names from a function.", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"license": "MIT", | ||
@@ -27,4 +27,4 @@ "repository": { | ||
"scripts": { | ||
"start:web": "vite-open example/web --https", | ||
"start:web:debugging": "vite-open example/web --https --debugging-this", | ||
"start:web": "devito example/web", | ||
"start:web:debugging": "devito example/web --debugging-this", | ||
"start:node": "onchange -i src example -- swcno example/node.ts", | ||
@@ -35,6 +35,4 @@ "build:watch": "fastpm autolink && tsc -p tsconfig.dist.json --outDir dist/types --watch & swc -w -C module.type=commonjs ./src -d dist/cjs -w & swc -w -C module.type=es6 ./src -d dist/esm", | ||
"clean": "rimraf dist", | ||
"docs": "dokio -o README.md && dprint fmt README.md", | ||
"test": "npm run test:node && npm run test:web", | ||
"test:node": "if find test -type f -iregex '.*\\.spec\\.\\(js\\|jsx\\|ts\\|tsx\\)$' | grep -q .; then utr 'test/*.spec.{js,jsx,ts,tsx}'; else echo no node tests; fi", | ||
"test:web": "if find test -type f -iregex '.*\\.spec\\.web\\.\\(js\\|jsx\\|ts\\|tsx\\)$' | grep -q .; then utr --browser 'test/*.spec.web.{js,jsx,ts,tsx}'; else echo no web tests; fi", | ||
"docs": "dokio -o README.md", | ||
"test": "utr", | ||
"cov": "utr --coverage", | ||
@@ -47,4 +45,4 @@ "build": "npm run build:cjs & npm run build:esm & npm run build:types && echo done.", | ||
"build:min": "bunzee -m src/index.ts \"dist/$(cat package.json | jq -r '.name').min.js\"", | ||
"lint": "eslint src && dprint check", | ||
"lint:fix": "eslint --fix src && dprint fmt", | ||
"lint": "eslint src", | ||
"lint:fix": "eslint --fix src", | ||
"prepack": "npm run clean && npm run build && (npm run build:dist || echo unable to bundle)", | ||
@@ -60,10 +58,12 @@ "prepack:dry": "npm pack --dry-run", | ||
"@tsconfig/node16": "1.0.3", | ||
"@types/audioworklet": "0.0.29", | ||
"@types/audioworklet": "0.0.30", | ||
"@types/jest": "27.5.2", | ||
"@types/node": "17.0.45", | ||
"@types/webmidi": "2.0.6", | ||
"@typescript-eslint/eslint-plugin": "5.30.7", | ||
"@typescript-eslint/parser": "5.30.7", | ||
"@typescript-eslint/eslint-plugin": "5.31.0", | ||
"@typescript-eslint/parser": "5.31.0", | ||
"bunzee": "^1.0.0", | ||
"dokio": "^0.0.2", | ||
"chokidar": "3.5.3", | ||
"devito": "^2.0.0", | ||
"dokio": "^1.1.0", | ||
"dprint": "0.30.3", | ||
@@ -73,16 +73,15 @@ "eslint": "8.20.0", | ||
"eslint-plugin-import": "2.26.0", | ||
"eslint-plugin-react": "7.30.0", | ||
"eslint-plugin-react": "7.30.1", | ||
"husky": "7.0.4", | ||
"onchange": "7.1.0", | ||
"pull-configs": "^0.2.0", | ||
"pull-configs": "^1.0.0", | ||
"rimraf": "3.0.2", | ||
"swcno": "0.1.1", | ||
"typescript": "4.7.3", | ||
"utr": "^0.2.0", | ||
"vite-open": "^3.0.1" | ||
"utr": "^1.3.3" | ||
}, | ||
"types": "./dist/types/index.d.ts", | ||
"dependencies": { | ||
"everyday-types": "^1.0.0" | ||
"everyday-types": "^1.1.0" | ||
} | ||
} |
@@ -0,3 +1,5 @@ | ||
<h1> | ||
argtor <a href="https://npmjs.org/package/argtor"><img src="https://img.shields.io/badge/npm-v2.0.0-F00.svg?colorA=000"/></a> <a href="src"><img src="https://img.shields.io/badge/loc-22-FFF.svg?colorA=000"/></a> <a href="https://cdn.jsdelivr.net/npm/argtor@2.0.0/dist/argtor.min.js"><img src="https://img.shields.io/badge/brotli-265b-333.svg?colorA=000"/></a> <a href="LICENSE"><img src="https://img.shields.io/badge/license-MIT-F0B.svg?colorA=000"/></a> | ||
argtor <a href="https://npmjs.org/package/argtor"><img src="https://img.shields.io/badge/npm-v3.0.0-F00.svg?colorA=000"/></a> <a href="src"><img src="https://img.shields.io/badge/loc-39-FFF.svg?colorA=000"/></a> <a href="https://cdn.jsdelivr.net/npm/argtor@3.0.0/dist/argtor.min.js"><img src="https://img.shields.io/badge/brotli-320b-333.svg?colorA=000"/></a> <a href="LICENSE"><img src="https://img.shields.io/badge/license-MIT-F0B.svg?colorA=000"/></a> | ||
</h1> | ||
@@ -19,16 +21,8 @@ | ||
## API | ||
<p> <details id="argtor$1" title="Function" open><summary><span><a href="#argtor$1">#</a></span> <code><strong>argtor</strong></code><em>(fn, keys)</em> – Extract destructured argument names from <code>fn</code>'s first argument and return a <code>Set</code> of <code>keys</code>.</summary> <a href="src/argtor.ts#L31">src/argtor.ts#L31</a> <ul> <p> <p> | ||
<p> <details id="argtor$1" title="Function" open><summary><span><a href="#argtor$1">#</a></span> <code><strong>argtor</strong></code><em>(fn, keys)</em> </summary> <a href=""></a> <ul> <p> <details id="fn$4" title="Parameter" ><summary><span><a href="#fn$4">#</a></span> <code><strong>fn</strong></code> </summary> <ul><p><span>Fn</span> & {<p> <details id="fn$6" title="Property" ><summary><span><a href="#fn$6">#</a></span> <code><strong>fn</strong></code> </summary> <a href=""></a> <ul><p><span>Fn</span></p> </ul></details></p>}</p> </ul></details><details id="keys$7" title="Parameter" ><summary><span><a href="#keys$7">#</a></span> <code><strong>keys</strong></code> <span><span> = </span> <code>...</code></span> </summary> <ul><p><span>Set</span><<a href="#T$3">T</a>></p> </ul></details> <p><strong>argtor</strong><<span>T</span>><em>(fn, keys)</em> => <ul><a href="#T$3">T</a> [] & {<p> <details id="source$9" title="Property" ><summary><span><a href="#source$9">#</a></span> <code><strong>source</strong></code> <span><span> = </span> <code>args.source</code></span> </summary> <a href=""></a> <ul><p>string</p> </ul></details></p>}</ul></p></p> </ul></details></p> | ||
```ts | ||
const fn = ({ foo, bar }) => void 0 | ||
argtor(fn) // => Set(2) {'foo', 'bar'} | ||
``` | ||
</p> | ||
<details id="fn$3" title="Function" ><summary><span><a href="#fn$3">#</a></span> <code><strong>fn</strong></code><em>(state)</em> </summary> <ul> <p> <details id="state$6" title="Parameter" ><summary><span><a href="#state$6">#</a></span> <code><strong>state</strong></code> </summary> <ul><p>any</p> </ul></details> <p><strong>fn</strong><em>(state)</em> => <ul>any</ul></p></p> </ul></details><details id="keys$7" title="Parameter" ><summary><span><a href="#keys$7">#</a></span> <code><strong>keys</strong></code> <span><span> = </span> <code>...</code></span> – A keys <code>Set</code> to fill in with the argument names.</summary> <ul><p><span>Set</span><string></p> </ul></details> <p><strong>argtor</strong><em>(fn, keys)</em> => <ul><span>Set</span><string></ul></p></p> </ul></details></p> | ||
## Credits | ||
- [everyday-types](https://npmjs.org/package/everyday-types) by [stagas](https://github.com/stagas) – Everyday utility types | ||
@@ -35,0 +29,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
22147
14.38%165
46.02%26
4%38
-13.64%Updated