diff --git a/README.md b/README.md index 15009e4..579dfca 100644 --- a/README.md +++ b/README.md @@ -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'; ``` `.is( [, options]);` diff --git a/bin/what.ts b/bin/what.ts index df7e69e..21754fa 100644 --- a/bin/what.ts +++ b/bin/what.ts @@ -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(); diff --git a/package.json b/package.json index 33d18e5..fc933ee 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,10 @@ "url": "https://apteryx.xyz/" }, "main": "./dist/index.js", + "exports": { + "require": "./dist/index.js", + "import": "./dist/index.mjs" + }, "files": [ "dist" ], @@ -17,13 +21,14 @@ }, "packageManager": "yarn@3.2.1", "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", @@ -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", @@ -71,9 +78,11 @@ "apteryx", "apteryx/typescript" ], - "ignorePatterns": ["test/*", "examples/*", "scripts/*", "dist/*"] - }, - "dependencies": { - "apteryx-prettier-config": "^1.1.0" + "ignorePatterns": [ + "test/*", + "examples/*", + "scripts/*", + "dist/*" + ] } } diff --git a/src/index.ts b/src/index.ts index dbd4212..0b2b317 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 { - 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 = {}): 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'; diff --git a/src/main.ts b/src/main.ts new file mode 100644 index 0000000..b769b47 --- /dev/null +++ b/src/main.ts @@ -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 { + 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 = {}): 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; diff --git a/yarn.lock b/yarn.lock index 8724768..ba07daa 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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" @@ -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" @@ -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" @@ -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 @@ -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"