Skip to content

Commit

Permalink
feat: export defaults and version
Browse files Browse the repository at this point in the history
  • Loading branch information
revelt committed Apr 10, 2022
1 parent bac85cf commit 7c27fb3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 36 deletions.
4 changes: 3 additions & 1 deletion packages/check-types-mini/README.md
Expand Up @@ -26,12 +26,14 @@

## Install

The latest version is **ESM only**: Node 12+ is needed to use it and it must be `import`ed instead of `require`d. If your project is not on ESM yet and you want to use `require`, use an older version of this program, `6.1.0`.
This package is ESM only: Node 12+ is needed to use it and it must be imported instead of required:

```bash
npm i check-types-mini
```

If you need a legacy version which works with `require`, use version 6.1.0

## Quick Take

```js
Expand Down
70 changes: 36 additions & 34 deletions packages/check-types-mini/src/main.ts
Expand Up @@ -5,7 +5,9 @@ import intersection from "lodash.intersection";
import { arrayiffy } from "arrayiffy-if-string";
import objectPath from "object-path";
import { isMatch } from "matcher";
import { version as v } from "../package.json";

const version: string = v;
declare let DEV: boolean;

interface Obj {
Expand Down Expand Up @@ -135,7 +137,7 @@ function internalApi(
let current = val !== undefined ? val : key;
DEV &&
console.log(
`138 ${`\u001b[${33}m${`current`}\u001b[${39}m`} = ${JSON.stringify(
`140 ${`\u001b[${33}m${`current`}\u001b[${39}m`} = ${JSON.stringify(
current,
null,
4
Expand All @@ -149,7 +151,7 @@ function internalApi(

DEV &&
console.log(
`152 FINAL ${`\u001b[${33}m${`tempObj`}\u001b[${39}m`} = ${JSON.stringify(
`154 FINAL ${`\u001b[${33}m${`tempObj`}\u001b[${39}m`} = ${JSON.stringify(
tempObj,
null,
4
Expand All @@ -164,7 +166,7 @@ function internalApi(

DEV &&
console.log(
`167 FINALLY, ${`\u001b[${33}m${`opts.schema`}\u001b[${39}m`} = ${JSON.stringify(
`169 FINALLY, ${`\u001b[${33}m${`opts.schema`}\u001b[${39}m`} = ${JSON.stringify(
opts.schema,
null,
4
Expand Down Expand Up @@ -220,12 +222,12 @@ function internalApi(
// During traversal, we'll check if each value is a plain object/array and
// match the keysets as well. However, traversal won't "see" root level keys.

DEV && console.log("223");
DEV && console.log("225");

if (opts.enforceStrictKeyset) {
DEV &&
console.log(
`228 so \u001b[${31}m${`opts.enforceStrictKeyset is ON`}\u001b[${39}m`
`230 so \u001b[${31}m${`opts.enforceStrictKeyset is ON`}\u001b[${39}m`
);
if (existy(opts.schema) && Object.keys(opts.schema).length > 0) {
if (
Expand All @@ -238,7 +240,7 @@ function internalApi(
opts.ignoreKeys
).length
) {
DEV && console.log("241");
DEV && console.log("243");
let keys = pullAll(
Object.keys(obj),
Object.keys(ref).concat(Object.keys(opts.schema))
Expand Down Expand Up @@ -291,14 +293,14 @@ function internalApi(
}
}

DEV && console.log("294:");
DEV && console.log("296:");

// 2. Call the monkey and traverse the schema object, checking each value-as-object
// or value-as-array separately, if opts.enforceStrictKeyset is on. Root level
// was checked in step 1. above. What's left is deeper levels.
DEV &&
console.log(
`301 ${`\u001b[${33}m${`LET'S CHECK obj`}\u001b[${39}m`} = ${JSON.stringify(
`303 ${`\u001b[${33}m${`LET'S CHECK obj`}\u001b[${39}m`} = ${JSON.stringify(
obj,
null,
4
Expand Down Expand Up @@ -332,7 +334,7 @@ function internalApi(

let ignoredPathsArr: string[] = [];

DEV && console.log(`335 TRAVERSAL STARTS`);
DEV && console.log(`337 TRAVERSAL STARTS`);
traverse(obj, (key, val, innerObj) => {
// innerObj.path
DEV &&
Expand All @@ -346,7 +348,7 @@ function internalApi(
}
DEV &&
console.log(
`349 \u001b[${36}m${`traversing: ██ ${innerObj.path} ██ ===========================`}\u001b[${39}m ${`\u001b[${33}m${`key`}\u001b[${39}m`} = ${key}; ${`\u001b[${33}m${`val`}\u001b[${39}m`} = ${`\u001b[${35}m${JSON.stringify(
`351 \u001b[${36}m${`traversing: ██ ${innerObj.path} ██ ===========================`}\u001b[${39}m ${`\u001b[${33}m${`key`}\u001b[${39}m`} = ${key}; ${`\u001b[${33}m${`val`}\u001b[${39}m`} = ${`\u001b[${35}m${JSON.stringify(
val,
null,
0
Expand All @@ -362,15 +364,15 @@ function internalApi(

DEV &&
console.log(
`365 ${`\u001b[${33}m${`opts.schema`}\u001b[${39}m`} = ${JSON.stringify(
`367 ${`\u001b[${33}m${`opts.schema`}\u001b[${39}m`} = ${JSON.stringify(
opts.schema,
null,
4
)}`
);
DEV &&
console.log(
`373 currently, ${`\u001b[${33}m${`ignoredPathsArr`}\u001b[${39}m`} = ${JSON.stringify(
`375 currently, ${`\u001b[${33}m${`ignoredPathsArr`}\u001b[${39}m`} = ${JSON.stringify(
ignoredPathsArr,
null,
4
Expand All @@ -385,7 +387,7 @@ function internalApi(
) {
DEV &&
console.log(
`388 \u001b[${32}m${`SKIP THIS PATH BECAUSE IT'S A CHILD OF IGNORED PATH`}\u001b[${39}m`
`390 \u001b[${32}m${`SKIP THIS PATH BECAUSE IT'S A CHILD OF IGNORED PATH`}\u001b[${39}m`
);
return current;
}
Expand All @@ -399,7 +401,7 @@ function internalApi(
) {
DEV &&
console.log(
`402 \u001b[${32}m${`SKIP THIS PATH BECAUSE ITS KEY (${objKey}), IS AMONG IGNORED (${JSON.stringify(
`404 \u001b[${32}m${`SKIP THIS PATH BECAUSE ITS KEY (${objKey}), IS AMONG IGNORED (${JSON.stringify(
opts.ignoreKeys,
null,
4
Expand All @@ -409,7 +411,7 @@ function internalApi(
}
DEV &&
console.log(
`412 key "${objKey}" was not skipped because it was not matcher-matched against ${JSON.stringify(
`414 key "${objKey}" was not skipped because it was not matcher-matched against ${JSON.stringify(
opts.ignoreKeys,
null,
4
Expand All @@ -424,7 +426,7 @@ function internalApi(
) {
DEV &&
console.log(
`427 \u001b[${32}m${`SKIP THIS PATH BECAUSE IT (${
`429 \u001b[${32}m${`SKIP THIS PATH BECAUSE IT (${
innerObj.path
}), IS AMONG IGNORED (${JSON.stringify(
opts.ignorePaths,
Expand All @@ -436,7 +438,7 @@ function internalApi(
}
DEV &&
console.log(
`439 path was not skipped because ${
`441 path was not skipped because ${
innerObj.path
} was not matcher-matched against ${JSON.stringify(
opts.ignorePaths,
Expand All @@ -454,7 +456,7 @@ function internalApi(
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ █
DEV &&
console.log(
`457: \n${`${`${`\u001b[${33}m${`██`}\u001b[${39}m`}${`\u001b[${31}m${`██`}\u001b[${39}m`}`}`.repeat(
`459: \n${`${`${`\u001b[${33}m${`██`}\u001b[${39}m`}${`\u001b[${31}m${`██`}\u001b[${39}m`}`}`.repeat(
10
)}\n`
);
Expand Down Expand Up @@ -513,7 +515,7 @@ function internalApi(
}
DEV &&
console.log(
`516 ${`\u001b[${32}m${`SET`}\u001b[${39}m`} ${`\u001b[${33}m${`optsSchemaHasThisPathDefined`}\u001b[${39}m`} = ${JSON.stringify(
`518 ${`\u001b[${32}m${`SET`}\u001b[${39}m`} ${`\u001b[${33}m${`optsSchemaHasThisPathDefined`}\u001b[${39}m`} = ${JSON.stringify(
optsSchemaHasThisPathDefined,
null,
4
Expand All @@ -526,7 +528,7 @@ function internalApi(
}
DEV &&
console.log(
`529 ${`\u001b[${32}m${`SET`}\u001b[${39}m`} ${`\u001b[${33}m${`refHasThisPathDefined`}\u001b[${39}m`} = ${JSON.stringify(
`531 ${`\u001b[${32}m${`SET`}\u001b[${39}m`} ${`\u001b[${33}m${`refHasThisPathDefined`}\u001b[${39}m`} = ${JSON.stringify(
refHasThisPathDefined,
null,
4
Expand Down Expand Up @@ -584,7 +586,7 @@ current = ${JSON.stringify(current, null, 4)}\n\n`
// be raw null/undefined, which would be arrayified and turned into string.
DEV &&
console.log(
`587 ${`\u001b[${33}m${`objectPath.get(opts.schema, innerObj.path)`}\u001b[${39}m`} = ${JSON.stringify(
`589 ${`\u001b[${33}m${`objectPath.get(opts.schema, innerObj.path)`}\u001b[${39}m`} = ${JSON.stringify(
objectPath.get(opts.schema, innerObj.path),
null,
4
Expand All @@ -595,7 +597,7 @@ current = ${JSON.stringify(current, null, 4)}\n\n`
);
DEV &&
console.log(
`598 ${`\u001b[${33}m${`currentKeysSchema`}\u001b[${39}m`} = ${JSON.stringify(
`600 ${`\u001b[${33}m${`currentKeysSchema`}\u001b[${39}m`} = ${JSON.stringify(
currentKeysSchema,
null,
4
Expand All @@ -613,7 +615,7 @@ current = ${JSON.stringify(current, null, 4)}\n\n`

DEV &&
console.log(
`616 ${`\u001b[${33}m${`currentKeysSchema`}\u001b[${39}m`} = ${JSON.stringify(
`618 ${`\u001b[${33}m${`currentKeysSchema`}\u001b[${39}m`} = ${JSON.stringify(
currentKeysSchema,
null,
4
Expand All @@ -627,13 +629,13 @@ current = ${JSON.stringify(current, null, 4)}\n\n`
!currentKeysSchema.includes(String(current)) &&
!currentKeysSchema.includes("boolean"))
) {
DEV && console.log("630 I. matching against schema.");
DEV && console.log("632 I. matching against schema.");
// new in v.2.2
// Check if key's value is array. Then, if it is, check if opts.acceptArrays is on.
// If it is, then iterate through the array, checking does each value conform to the
// types listed in that key's schema entry.
if (Array.isArray(current) && opts.acceptArrays) {
DEV && console.log("636 1-1: check acceptArrays");
DEV && console.log("638 1-1: check acceptArrays");
// check each key:
for (let i = 0, len = current.length; i < len; i++) {
if (!currentKeysSchema.includes(typ(current[i]).toLowerCase())) {
Expand All @@ -653,7 +655,7 @@ current = ${JSON.stringify(current, null, 4)}\n\n`
}
}
} else {
DEV && console.log("656 1-2: matching against schema");
DEV && console.log("658 1-2: matching against schema");
// only then do throw...
throw new TypeError(
`${opts.msg}: ${opts.optsVarName}.${
Expand All @@ -675,7 +677,7 @@ current = ${JSON.stringify(current, null, 4)}\n\n`
} else {
DEV &&
console.log(
`678 names were blanket: ${JSON.stringify(
`680 names were blanket: ${JSON.stringify(
intersection(currentKeysSchema, NAMESFORANYTYPE),
null,
4
Expand All @@ -684,7 +686,7 @@ current = ${JSON.stringify(current, null, 4)}\n\n`
ignoredPathsArr.push(innerObj.path);
DEV &&
console.log(
`687 ${`\u001b[${33}m${`ignoredPathsArr`}\u001b[${39}m`} = ${JSON.stringify(
`689 ${`\u001b[${33}m${`ignoredPathsArr`}\u001b[${39}m`} = ${JSON.stringify(
ignoredPathsArr,
null,
4
Expand Down Expand Up @@ -719,7 +721,7 @@ current = ${JSON.stringify(current, null, 4)}\n\n`
Array.isArray(current) &&
!(opts.acceptArraysIgnore as string[]).includes(key)
) {
DEV && console.log("722 2-1: check accept arrays");
DEV && console.log("724 2-1: check accept arrays");
let allMatch = current.every(
(el) => typ(el).toLowerCase() === typ((ref as Obj)[key]).toLowerCase()
);
Expand All @@ -733,7 +735,7 @@ current = ${JSON.stringify(current, null, 4)}\n\n`
);
}
} else if (typ(current) !== typ(compareTo)) {
DEV && console.log("736 - 2-2: match against ref");
DEV && console.log("738 - 2-2: match against ref");
throw new TypeError(
`${opts.msg}: ${opts.optsVarName}.${
innerObj.path
Expand All @@ -747,15 +749,15 @@ current = ${JSON.stringify(current, null, 4)}\n\n`
);
}
} else {
DEV && console.log("750 do nothing");
DEV && console.log("752 do nothing");
}

DEV && console.log(`753 return: ${JSON.stringify(current, null, 4)}`);
DEV && console.log(`755 return: ${JSON.stringify(current, null, 4)}`);
return current;
});
DEV &&
console.log(
`758 ${`${`\u001b[${32}m${`█`}\u001b[${39}m`} `.repeat(
`760 ${`${`\u001b[${32}m${`█`}\u001b[${39}m`} `.repeat(
39
)} TRAVERSAL ENDS\n\n\n`
);
Expand All @@ -772,4 +774,4 @@ function checkTypesMini(
internalApi(obj, ref, originalOptions);
}

export { checkTypesMini };
export { checkTypesMini, defaults, version };
4 changes: 3 additions & 1 deletion packages/check-types-mini/types/index.d.ts
@@ -1,3 +1,4 @@
declare const version: string;
interface Obj {
[key: string]: any;
}
Expand All @@ -11,6 +12,7 @@ interface Opts {
msg: string;
optsVarName: string;
}
declare const defaults: Opts;
/**
* Validate options object
*/
Expand All @@ -20,4 +22,4 @@ declare function checkTypesMini(
originalOptions?: Partial<Opts>
): void;

export { checkTypesMini };
export { checkTypesMini, defaults, version };

0 comments on commit 7c27fb3

Please sign in to comment.