Skip to content

Commit

Permalink
Updated README and documented code
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofZawisla committed Apr 19, 2021
1 parent e984cb5 commit 49ed75d
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 70 deletions.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ os:
language: node_js
node_js:
- node
- 15
- 14
- 13
- 12
- 11
- 10
cache: cargo
before_install:
- curl https://sh.rustup.rs -sSf > /tmp/rustup.sh
Expand Down
199 changes: 133 additions & 66 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,72 +28,6 @@ Via pnpm:
pnpm i grex.js
```

## API Reference

```ts
export const enum ConversionOfEnum {
Digit = "digit",
NoDigit = "noDigit",
Space = "space",
NoSpace = "noSpace",
Word = "word",
NoWord = "noWord",
Repetition = "repetition",
CaseInsensitivity = "caseInsensitivity",
CapturingGroup = "capturingGroup",
}

export type ConversionOf =
| "digit"
| "noDigit"
| "space"
| "noSpace"
| "word"
| "noWord"
| "repetition"
| "caseInsensitivity"
| "capturingGroup";

export type BuildRegex = (testCaces: string[], config?: Config) => string;

/** GrexJS wrapper interface */
export interface GrexJS {
/**
* Basic function to build Regex based on input array
*
* @example
* const regex: string = buildRegex(["a", "aa", "aaa"]);
* console.log(regex); // "^a(?:aa?)?$"
* @param {Array<ConversionOf | ConversionOfEnum>} testCaces
* @param {Config} config
* @returns {string} Returns regular expresion
*/
buildRegex: BuildRegex;
}

/** Config type */
export interface Config {
conversionOf?: (ConversionOf | ConversionOfEnum)[];
minimumRepetitions?: number;
syntaxHighlighting?: boolean;
escapedNonASCIIChars?: boolean;
surrogatePairs?: boolean;
minimumSubstringLength?: number;
}

// Avaible only in wasm version
export type Load = () => Promise<BuildRegex>;

// Avaible only in wasm version
export default load: Load;

// Avaible only in native version
export default grexJS: GrexJS;

// Avaible only in native version
export const buildRegex: BuildRegex;
```

## Usage

### With wasm module for browser
Expand Down Expand Up @@ -152,6 +86,11 @@ Function instantiation has the same logic as version for the browser.

### Usage with native module for node

MacOS and Linux support:
NodeJS version equal or higher than: `10`.
Windows support:
NodeJS version equal or higher than: `15`.

Import:

```ts
Expand All @@ -173,3 +112,131 @@ const { buildRegex }: GrexJS = await import("grex.js/native");
```

Also works with `amd, commonjs and system` library targets.

## API Reference

```ts
/** Enum of available options in param: `conversionOf` of `config` argument
*
* @example
* const regex: string = buildRegex(["a", "aa", "123"], {
* conversionOf: [ConversionOfEnum.Digit, ConversionOfEnum.Word]
* });
*/
export const enum ConversionOfEnum {
Digit = "digit",
NoDigit = "noDigit",
Space = "space",
NoSpace = "noSpace",
Word = "word",
NoWord = "noWord",
Repetition = "repetition",
CaseInsensitivity = "caseInsensitivity",
CapturingGroup = "capturingGroup",
}

/** Type of `conversionOf` param of the `config` argument
*
* @see Config, buildRegex, BuildRegex
*/
export type ConversionOf =
| "digit"
| "noDigit"
| "space"
| "noSpace"
| "word"
| "noWord"
| "repetition"
| "caseInsensitivity"
| "capturingGroup";

/** Type of `buildRegex` function
*
* @see buildRegex
*/
export type BuildRegex = (testCaces: string[], config?: Config) => string;

/** GrexJS wrapper interface */
export interface GrexJS {
/**
* Function to build Regex based on input array
*
* @example
* const regex: string = buildRegex(["a", "aa", "aaa"]);
* console.log(regex); // "^a(?:aa?)?$"
* @param {Array<ConversionOf | ConversionOfEnum>} testCaces
* @param {Config} config
* @returns {string} Returns regular expresion
* @type {BuildRegex}
* @see @link https://github.com/pemistahl/grex#52--the-library-top-
*/
buildRegex: BuildRegex;
}

/** Config type
*
* @see buildRegex BuildRegex
* @see @link https://github.com/pemistahl/grex#52--the-library-top-
*/
export interface Config {
/**
* @see @link https://github.com/pemistahl/grex#52--the-library-top-
*/
conversionOf?: (ConversionOf | ConversionOfEnum)[];
/**
* @see @link https://github.com/pemistahl/grex#523-convert-repeated-substrings
*/
minimumRepetitions?: number;
/**
* @see @link https://github.com/pemistahl/grex#528-syntax-highlighting
*/
syntaxHighlighting?: boolean;
/**
* @see @link https://github.com/pemistahl/grex#524-escape-non-ascii-characters
*/
escapedNonASCIIChars?: boolean;
/**
* @see @link https://github.com/pemistahl/grex#524-escape-non-ascii-characters
*/
surrogatePairs?: boolean;
/**
* @see @link https://github.com/pemistahl/grex#523-convert-repeated-substrings
*/
minimumSubstringLength?: number;
}

/** Load function type */
export type Load = () => Promise<BuildRegex>; // avaible only in wasm module

/** Loads and instantiates `buildRegex` function
*
* @example
* import { load, BuildRegex } from "grex.js";
* const buildRegex: BuildRegex = await load();
* @example
* import { load, BuildRegex } from "grex.js";
* (async (): void => {
* const buildRegex: BuildRegex = await load();
* })();
* @type {Load}
* @returns {Promise<BuildRegex>} returns `Promise` with imported and instantiated `buildRegex` function
*/
export (default) const load: Load = async () => Promise<BuildRegex>; // avaible only in wasm module

/**
* Function to build Regex based on input array
*
* @example
* const regex: string = buildRegex(["a", "aa", "aaa"]);
* console.log(regex); // "^a(?:aa?)?$"
* @param {Array<ConversionOf | ConversionOfEnum>} testCaces
* @param {Config} config
* @returns {string} Returns regular expresion
* @type {BuildRegex}
* @see @link https://github.com/pemistahl/grex#52--the-library-top-
*/
export const buildRegex: BuildRegex = grexJS.buildRegex;

/** GrexJS wrapper */
export default grexJS: GrexJS;
```
6 changes: 4 additions & 2 deletions src/native/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ export type { ConversionOfEnum, ConversionOf, BuildRegex, GrexJS, Config };
const grexJS: GrexJS = grexJSAddon as GrexJS;

/**
* Basic function to build Regex based on input array
* Function to build Regex based on input array
*
* @example
* const regex: string = buildRegex(["a", "aa", "aaa"]);
* console.log(regex); // "^a(?:aa?)?$"
* @param {Array<ConversionOf | ConversionOfEnum>} testCaces
* @param {Config} config
* @returns {string} Returns regular expresion.
* @returns {string} Returns regular expresion
* @type {BuildRegex}
* @see @link https://github.com/pemistahl/grex#52--the-library-top-
*/
export const buildRegex: BuildRegex = grexJS.buildRegex;

Expand Down
1 change: 1 addition & 0 deletions src/types-wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ import type {
} from "./types";
export type { ConversionOfEnum, ConversionOf, BuildRegex, Config, GrexJS };

/** Load function type */
export type Load = () => Promise<BuildRegex>;
43 changes: 41 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/** Enum of available options in param: `conversionOf` of `config` argument
*
* @example
* const regex: string = buildRegex(["a", "aa", "123"], {
* conversionOf: [ConversionOfEnum.Digit, ConversionOfEnum.Word]
* });
*/
export const enum ConversionOfEnum {
Digit = "digit",
NoDigit = "noDigit",
Expand All @@ -10,6 +17,10 @@ export const enum ConversionOfEnum {
CapturingGroup = "capturingGroup",
}

/** Type of `conversionOf` param of the `config` argument
*
* @see Config, buildRegex, BuildRegex
*/
export type ConversionOf =
| "digit"
| "noDigit"
Expand All @@ -21,29 +32,57 @@ export type ConversionOf =
| "caseInsensitivity"
| "capturingGroup";

/** Type of `buildRegex` function
*
* @see buildRegex
*/
export type BuildRegex = (testCaces: string[], config?: Config) => string;

/** GrexJS wrapper interface */
export interface GrexJS {
/**
* Basic function to build Regex based on input array
* Function to build Regex based on input array
*
* @example
* const regex: string = buildRegex(["a", "aa", "aaa"]);
* console.log(regex); // "^a(?:aa?)?$"
* @param {Array<ConversionOf | ConversionOfEnum>} testCaces
* @param {Config} config
* @returns {string} Returns regular expresion
* @type {BuildRegex}
* @see @link https://github.com/pemistahl/grex#52--the-library-top-
*/
buildRegex: BuildRegex;
}

/** Config type */
/** Config type
*
* @see buildRegex BuildRegex
* @see @link https://github.com/pemistahl/grex#52--the-library-top-
*/
export interface Config {
/**
* @see @link https://github.com/pemistahl/grex#52--the-library-top-
*/
conversionOf?: (ConversionOf | ConversionOfEnum)[];
/**
* @see @link https://github.com/pemistahl/grex#523-convert-repeated-substrings
*/
minimumRepetitions?: number;
/**
* @see @link https://github.com/pemistahl/grex#528-syntax-highlighting
*/
syntaxHighlighting?: boolean;
/**
* @see @link https://github.com/pemistahl/grex#524-escape-non-ascii-characters
*/
escapedNonASCIIChars?: boolean;
/**
* @see @link https://github.com/pemistahl/grex#524-escape-non-ascii-characters
*/
surrogatePairs?: boolean;
/**
* @see @link https://github.com/pemistahl/grex#523-convert-repeated-substrings
*/
minimumSubstringLength?: number;
}
13 changes: 13 additions & 0 deletions src/wasm/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ export type {
Load,
};

/** Loads and instantiates `buildRegex` function
*
* @example
* import { load, BuildRegex } from "grex.js";
* const buildRegex: BuildRegex = await load();
* @example
* import { load, BuildRegex } from "grex.js";
* (async (): void => {
* const buildRegex: BuildRegex = await load();
* })();
* @type {Load}
* @returns {Promise<BuildRegex>} returns `Promise` with imported and instantiated `buildRegex` function
*/
export const load: Load = async (): Promise<BuildRegex> => {
return (await ((await import("../native/pkg-browser")) as Promise<GrexJS>))
.buildRegex;
Expand Down
13 changes: 13 additions & 0 deletions src/wasm/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ export type {
Load,
};

/** Loads and instantiates `buildRegex` function
*
* @example
* import { load, BuildRegex } from "grex.js";
* const buildRegex: BuildRegex = await load();
* @example
* import { load, BuildRegex } from "grex.js";
* (async (): void => {
* const buildRegex: BuildRegex = await load();
* })();
* @type {Load}
* @returns {Promise<BuildRegex>} returns `Promise` with imported and instantiated `buildRegex` function
*/
export const load: Load = async (): Promise<BuildRegex> => {
return (await ((await import("../native/pkg-node")) as Promise<GrexJS>))
.buildRegex;
Expand Down

0 comments on commit 49ed75d

Please sign in to comment.