Skip to content

Commit

Permalink
fix: requiring and importing module
Browse files Browse the repository at this point in the history
  • Loading branch information
apteryxxyz committed Sep 22, 2022
1 parent 19a1cbd commit 2d5dd9a
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 67 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ The node module is very simple to use, containing only a single method.
```ts
const what = require('jswhat');
// OR
import * as what from 'jswhat';
import what from 'jswhat';
```

`<what>.is(<inputs> [, options]);`
Expand Down
2 changes: 1 addition & 1 deletion bin/what.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#! /usr/bin/env node

import * as what from '../src';
import what from '../src';
import type { Match } from '../src';
const timer = Date.now();

Expand Down
25 changes: 17 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
"url": "https://apteryx.xyz/"
},
"main": "./dist/index.js",
"exports": {
"require": "./dist/index.js",
"import": "./dist/index.mjs"
},
"files": [
"dist"
],
Expand All @@ -17,13 +21,14 @@
},
"packageManager": "[email protected]",
"scripts": {
"clean": "rm -rf dist temp",
"lint": "eslint {src,bin} --fix",
"format": "prettier src bin --write",
"test": "node test/identify.test",
"prebuild": "tsc -p tsconfig.json && webpack --config webpack.config.js",
"build": "yarn prebuild && yarn postbuild",
"postbuild": "cp temp/bin/what.js dist/what.js && node scripts/replaceBinPaths && rm -rf temp"
"build:compile": "tsc && webpack",
"build:copy": "cp temp/bin/what.js dist/what.js && node scripts/replaceBinPaths",
"build:module": "gen-esm-wrapper . ./dist/index.mjs",
"build:clean": "rm -rf temp",
"build": "yarn build:compile && yarn build:copy && yarn build:module && yarn build:clean"
},
"repository": {
"type": "git",
Expand All @@ -50,12 +55,14 @@
"@types/web": "^0.0.67",
"@typescript-eslint/eslint-plugin": "^5.38.0",
"@typescript-eslint/parser": "^5.38.0",
"apteryx-prettier-config": "^1.1.0",
"babel-loader": "^8.2.5",
"bundle-declarations-webpack-plugin": "^3.1.0",
"eslint": "^8.23.1",
"eslint-config-apteryx": "^1.0.4",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.2.1",
"gen-esm-wrapper": "^1.1.3",
"prettier": "^2.7.1",
"tslib": "^2.4.0",
"webpack": "^5.73.0",
Expand All @@ -71,9 +78,11 @@
"apteryx",
"apteryx/typescript"
],
"ignorePatterns": ["test/*", "examples/*", "scripts/*", "dist/*"]
},
"dependencies": {
"apteryx-prettier-config": "^1.1.0"
"ignorePatterns": [
"test/*",
"examples/*",
"scripts/*",
"dist/*"
]
}
}
64 changes: 7 additions & 57 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,9 @@
import pkg from '../package.json';
import { identify } from './identify';
export * from './identify';
import type { Match, Options } from './types';
export * from './types';

const defaultOptions: Options = {
search: false,
exclude: [],
filter: [],
rarity: [0, 1],
};

// Combine options with the defaults and verify that they are valid
function combineAndVerifyOptions(rawOptions: Partial<Options>): Options {
const options = { ...defaultOptions, ...rawOptions };

if (typeof options.search !== 'boolean') {
throw new TypeError("Invalid 'options.search', must be a boolean");
} else if (
!Array.isArray(options.exclude) ||
options.exclude.some(x => typeof x !== 'string')
) {
throw new TypeError("Invalid 'options.exclude', must be an array of strings");
} else if (!Array.isArray(options.filter) || options.filter.some(x => typeof x !== 'string')) {
throw new TypeError("Invalid 'options.filter', must be an array of strings");
} else if (
!Array.isArray(options.rarity) ||
options.rarity.some(x => typeof x !== 'number' || x < 0 || x > 1) ||
options.rarity[0] > options.rarity[1]
) {
throw new TypeError(
"Invalid 'options.rarity', must be an array of two numbers, from 0 and 1"
);
}
import * as what from './main';

return options;
}
export default what;
export const Regexes = what.Regexes;
export const identify = what.identify;
export const is = what.is;
export const version = what.version;

/**
* Detect what a string is.
* @param input The input string to search for matches
* @param options Options to pass to the search
*/
export function is(input: string | string[], options: Partial<Options> = {}): Match[] {
const inputs = [input].flat();
if (inputs.some(i => typeof i !== 'string')) {
throw new TypeError('Input must be a string or an array of strings');
}

const fOptions = combineAndVerifyOptions(options);
if (inputs.length === 0) return [];
return identify(inputs, fOptions);
}

/**
* The version of the library.
*/
export const { version } = pkg;
export * from './types';
58 changes: 58 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import pkg from '../package.json';
import { identify } from './identify';
export * from './identify';
import type { Match, Options } from './types';

const defaultOptions: Options = {
search: false,
exclude: [],
filter: [],
rarity: [0, 1],
};

// Combine options with the defaults and verify that they are valid
function combineAndVerifyOptions(rawOptions: Partial<Options>): Options {
const options = { ...defaultOptions, ...rawOptions };

if (typeof options.search !== 'boolean') {
throw new TypeError("Invalid 'options.search', must be a boolean");
} else if (
!Array.isArray(options.exclude) ||
options.exclude.some(x => typeof x !== 'string')
) {
throw new TypeError("Invalid 'options.exclude', must be an array of strings");
} else if (!Array.isArray(options.filter) || options.filter.some(x => typeof x !== 'string')) {
throw new TypeError("Invalid 'options.filter', must be an array of strings");
} else if (
!Array.isArray(options.rarity) ||
options.rarity.some(x => typeof x !== 'number' || x < 0 || x > 1) ||
options.rarity[0] > options.rarity[1]
) {
throw new TypeError(
"Invalid 'options.rarity', must be an array of two numbers, from 0 and 1"
);
}

return options;
}

/**
* Detect what a string is.
* @param input The input string to search for matches
* @param options Options to pass to the search
*/
export function is(input: string | string[], options: Partial<Options> = {}): Match[] {
const inputs = [input].flat();
if (inputs.some(i => typeof i !== 'string')) {
throw new TypeError('Input must be a string or an array of strings');
}

const fOptions = combineAndVerifyOptions(options);
if (inputs.length === 0) return [];
return identify(inputs, fOptions);
}

/**
* The version of the library.
*/
export const { version } = pkg;
47 changes: 47 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1966,6 +1966,16 @@ __metadata:
languageName: node
linkType: hard

"assert@npm:^1.4.1":
version: 1.5.0
resolution: "assert@npm:1.5.0"
dependencies:
object-assign: ^4.1.1
util: 0.10.3
checksum: 9be48435f726029ae7020c5888a3566bf4d617687aab280827f2e4029644b6515a9519ea10d018b342147c02faf73d9e9419e780e8937b3786ee4945a0ca71e5
languageName: node
linkType: hard

"ast-types-flow@npm:^0.0.7":
version: 0.0.7
resolution: "ast-types-flow@npm:0.0.7"
Expand Down Expand Up @@ -3057,6 +3067,17 @@ __metadata:
languageName: node
linkType: hard

"gen-esm-wrapper@npm:^1.1.3":
version: 1.1.3
resolution: "gen-esm-wrapper@npm:1.1.3"
dependencies:
is-valid-identifier: ^2.0.2
bin:
gen-esm-wrapper: gen-esm-wrapper.js
checksum: 600abe05141d3a3d71af3bd65a1967c7fd09bd7c60d487dca5c30b2af0f58b40580990d34cfa3ac1dffd78463d895912a100bac4ea6e8cb2ee4461bbafa67011
languageName: node
linkType: hard

"gensync@npm:^1.0.0-beta.2":
version: 1.0.0-beta.2
resolution: "gensync@npm:1.0.0-beta.2"
Expand Down Expand Up @@ -3308,6 +3329,13 @@ __metadata:
languageName: node
linkType: hard

"inherits@npm:2.0.1":
version: 2.0.1
resolution: "inherits@npm:2.0.1"
checksum: 6536b9377296d4ce8ee89c5c543cb75030934e61af42dba98a428e7d026938c5985ea4d1e3b87743a5b834f40ed1187f89c2d7479e9d59e41d2d1051aefba07b
languageName: node
linkType: hard

"internal-slot@npm:^1.0.3":
version: 1.0.3
resolution: "internal-slot@npm:1.0.3"
Expand Down Expand Up @@ -3471,6 +3499,15 @@ __metadata:
languageName: node
linkType: hard

"is-valid-identifier@npm:^2.0.2":
version: 2.0.2
resolution: "is-valid-identifier@npm:2.0.2"
dependencies:
assert: ^1.4.1
checksum: 79e5237998621f09b76582d8ef6928eb4cc96e78795d317aa1ea260aa5d22d4dae3c5baa519414f8e9a4833ee948e3bf0bd98496802f492c30640d672b528117
languageName: node
linkType: hard

"is-weakref@npm:^1.0.2":
version: 1.0.2
resolution: "is-weakref@npm:1.0.2"
Expand Down Expand Up @@ -3607,6 +3644,7 @@ __metadata:
eslint-config-apteryx: ^1.0.4
eslint-config-prettier: ^8.5.0
eslint-plugin-prettier: ^4.2.1
gen-esm-wrapper: ^1.1.3
prettier: ^2.7.1
tslib: ^2.4.0
webpack: ^5.73.0
Expand Down Expand Up @@ -4789,6 +4827,15 @@ __metadata:
languageName: node
linkType: hard

"util@npm:0.10.3":
version: 0.10.3
resolution: "util@npm:0.10.3"
dependencies:
inherits: 2.0.1
checksum: bd800f5d237a82caddb61723a6cbe45297d25dd258651a31335a4d5d981fd033cb4771f82db3d5d59b582b187cb69cfe727dc6f4d8d7826f686ee6c07ce611e0
languageName: node
linkType: hard

"watchpack@npm:^2.3.1":
version: 2.4.0
resolution: "watchpack@npm:2.4.0"
Expand Down

0 comments on commit 2d5dd9a

Please sign in to comment.