-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
19a1cbd
commit 2d5dd9a
Showing
6 changed files
with
131 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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": "[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", | ||
|
@@ -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/*" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters