# literal-toolkit

> A toolkit to parse and generate JavaScript style literals.

Latest version **1.3.1** (published 2023-06-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install literal-toolkit
pnpm add literal-toolkit
yarn add literal-toolkit
bun add literal-toolkit
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.1 |
| Published | 2023-06-03 |
| First published | 2018-12-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 30.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | A-yon Lee |
| Maintainers | ayonli |
| Keywords | string, number, boolean, regexp, comment, literal |

## Links

- npm: https://www.npmjs.com/package/literal-toolkit
- Repository: https://github.com/ayonli/literal-toolkit
- Homepage: https://github.com/ayonli/literal-toolkit#readme
- Issues: https://github.com/ayonli/literal-toolkit/issues
- npm.io page: https://npm.io/package/literal-toolkit

## Dependencies (1)

- [safe-string-literal](https://npm.io/package/safe-string-literal.md) ^1.0.5

## Alternatives

- [random-seedable](https://npm.io/package/random-seedable.md) — 27.9K weekly downloads
- [n2words](https://npm.io/package/n2words.md) — 22.2K weekly downloads
- [@stdlib/math-base-special-factorialln](https://npm.io/package/@stdlib/math-base-special-factorialln.md) — 5.7K weekly downloads
- [@stdlib/math-base-special-abs2](https://npm.io/package/@stdlib/math-base-special-abs2.md) — 1.7K weekly downloads
- [commons-math-interpolation](https://npm.io/package/commons-math-interpolation.md) — 1.4K weekly downloads

## Recent versions

- 1.3.1 (latest) — 2023-06-03
- 1.3.0 — 2019-12-02
- 1.2.10 — 2019-05-25
- 1.2.9 — 2019-05-24
- 1.2.8 — 2019-05-24
- 1.2.7 — 2019-01-24
- 1.2.6 — 2019-01-23
- 1.2.5 — 2019-01-21
- 1.2.4 — 2019-01-19
- 1.2.3 — 2019-01-19
- 1.2.2 — 2019-01-19
- 1.2.1 — 2019-01-19
- 1.2.0 — 2019-01-19
- 1.1.2 — 2019-01-16
- 1.1.1 — 2019-01-11
- … 3 more at https://npm.io/package/literal-toolkit/versions

## README

# Literal Toolkit

**A toolkit to parse and generate JavaScript style literals.**

In case to write a pseudo code implementation or data structure, this toolkit 
will help a lot on parsing and generating strings, numbers, regular expressions,
keyword values, and even comments.

## Install

```sh
$ npm i literal-toolkit
```

## API

There are several interfaces under this package, each of them have the similar 
functions that can be used to parse and generate literals.

- `LiteralToken`
    - `source: string` will exclude any leading spaces.
    - `offset: number` the index position where `source` starts.
    - `length: number` the length of the `source` string.

- `string`
    - `StringToken` extends `LiteralToken`
        - `value: string`
        - <code>quote: "'" | "\"" | "`"</code>
    - `parse(str: string): string`
    - `parseToken(str: string): StringToken`
    - <code>toLiteral(str: string, quote?: "'" | "\"" | "`"): string</code>

- `number`
    - `NumberToken` extends `LiteralToken`
        - `value: number | bigint`
        - `radix: 2 | 8 | 10 | 16`
    - `parse(str: string, strict?: boolean): number | bigint`
    - `parseToken(str: string): NumberToken`
    - `isBin(str: string): boolean`
    - `isOct(str: string): boolean`
    - `isDec(str: string): boolean`
    - `isHex(str: string): boolean`
    - `isNaN(str: string): boolean`
    - `isFinite(str: string): boolean`
    - `isBigInt(str: string): boolean`
    - `toLiteral(num: number | bigint, radix?: 2 | 8 | 10 | 16): string`

- `keyword` Includes `true`, `false`, `null`, `NaN` and `Infinity`
    - `KeywordToken` extends `LiteralToken`
        - `value: true | false | null | number`
    - `parse(str: string): KeywordToken["value"]`
    - `parseToken(str: string): KeywordToken`
    - `toLiteral(keyword: KeywordToken["value"]): string`

- `regexp`
    - `RegExpToken` extends `LiteralToken`
        - `value: RegExp`
    - `parse(str: string): RegExp`
    - `parseToken(str: string): RegExpToken`
    - `toLiteral(re: RegExp): string`

- `comment`
    - `CommentToken` extends `LiteralToken`
        - `value: string`
        - `type: "//" | "/*" | "/**"`
    - `parse(str: string, strip?: boolean): string`
    - `parseToken(str: string): CommentToken`
    - `toLiteral(str: string, type?: "//" | "/*" | "/**", inden?: string): string`

All `parseToken()` functions, when the given string cannot be parsed, will 
return `null` by default.

All `parse()` functions are short-cuts of `parseToken(str).value` (might include
additional features). All these functions, when the given string cannot be 
parsed, will return `undefined` instead.

All `parse()` functions are just for simple parsing usage, when dealing with 
complex scenarios, use `parseToken()` instead.

For detailed API documentation, please redirect to [interface declarations](./index.d.ts).

## Usage

```javascript
import { string, number, keyword, regexp, comment } from "literal-toolkit";

string.parse('"this is a double-quoted string literal"');
string.parse("'this is a single-quoted string literal'");
string.parse("`this is a back-quoted\n and multi-line string`");

number.parse("1234567"); // decimal number: 1234567
number.parse("0b1010101"); // binary number: 0b1010101
number.parse("0o1234567"); // octal number: 0o1234567
number.parse("01234567"); // octal number without 'o': 01234567
number.parse("0x1234567"); // hexadecimal number: 0x1234567

keyword.parse("true"); // boolean: true
keyword.parse("false"); // boolean: false
keyword.parse("null"); // null
keyword.parse("NaN"); // number: NaN
keyword.parse("Infinity"); // number: Infinity

regexp.parse("/[a-zA-Z0-9]/i"); // RegExp: /[a-zA-Z0-9]/i

comment.parse("// this is a single-line comment");
comment.parse("/* this is a inline comment */");
comment.parse("/* this comment contains\n multiple\n lines */");
comment.parse("/** this is a JSDoc comment */");
```

This toolkit is meant to parse any valid JavaScript literal strings (of 
supported types) into real values, so any form that works in JavaScript syntax 
can be parsed by this package, although the above example doesn't cover that 
much. Check the [test](./test) for more examples.

---
_Source: https://npm.io/package/literal-toolkit · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
