comma-separated-tokens
Advanced tools
Comparing version 2.0.2 to 2.0.3
/** | ||
* @typedef {Object} StringifyOptions | ||
* @property {boolean} [padLeft=true] Whether to pad a space before a token (`boolean`, default: `true`). | ||
* @property {boolean} [padRight=false] Whether to pad a space after a token (`boolean`, default: `false`). | ||
* @typedef Options | ||
* Configuration for `stringify`. | ||
* @property {boolean} [padLeft=true] | ||
* Whether to pad a space before a token. | ||
* @property {boolean} [padRight=false] | ||
* Whether to pad a space after a token. | ||
*/ | ||
/** | ||
* Parse comma separated tokens to an array. | ||
* @typedef {Options} StringifyOptions | ||
* Please use `StringifyOptions` instead. | ||
*/ | ||
/** | ||
* Parse comma-separated tokens to an array. | ||
* | ||
* @param {string} value | ||
* @returns {Array.<string>} | ||
* Comma-separated tokens. | ||
* @returns {Array<string>} | ||
* List of tokens. | ||
*/ | ||
export function parse(value: string): Array<string> | ||
/** | ||
* Serialize an array of strings to comma separated tokens. | ||
* Serialize an array of strings or numbers to comma-separated tokens. | ||
* | ||
* @param {Array.<string|number>} values | ||
* @param {StringifyOptions} [options] | ||
* @param {Array<string|number>} values | ||
* List of tokens. | ||
* @param {Options} [options] | ||
* Configuration for `stringify` (optional). | ||
* @returns {string} | ||
* Comma-separated tokens. | ||
*/ | ||
export function stringify( | ||
values: Array<string | number>, | ||
options?: StringifyOptions | ||
options?: Options | undefined | ||
): string | ||
export type StringifyOptions = { | ||
/** | ||
* Configuration for `stringify`. | ||
*/ | ||
export type Options = { | ||
/** | ||
* Whether to pad a space before a token (`boolean`, default: `true`). | ||
* Whether to pad a space before a token. | ||
*/ | ||
padLeft?: boolean | ||
padLeft?: boolean | undefined | ||
/** | ||
* Whether to pad a space after a token (`boolean`, default: `false`). | ||
* Whether to pad a space after a token. | ||
*/ | ||
padRight?: boolean | ||
padRight?: boolean | undefined | ||
} | ||
/** | ||
* Please use `StringifyOptions` instead. | ||
*/ | ||
export type StringifyOptions = Options |
53
index.js
/** | ||
* @typedef {Object} StringifyOptions | ||
* @property {boolean} [padLeft=true] Whether to pad a space before a token (`boolean`, default: `true`). | ||
* @property {boolean} [padRight=false] Whether to pad a space after a token (`boolean`, default: `false`). | ||
* @typedef Options | ||
* Configuration for `stringify`. | ||
* @property {boolean} [padLeft=true] | ||
* Whether to pad a space before a token. | ||
* @property {boolean} [padRight=false] | ||
* Whether to pad a space after a token. | ||
*/ | ||
/** | ||
* Parse comma separated tokens to an array. | ||
* @typedef {Options} StringifyOptions | ||
* Please use `StringifyOptions` instead. | ||
*/ | ||
/** | ||
* Parse comma-separated tokens to an array. | ||
* | ||
* @param {string} value | ||
* @returns {Array.<string>} | ||
* Comma-separated tokens. | ||
* @returns {Array<string>} | ||
* List of tokens. | ||
*/ | ||
export function parse(value) { | ||
/** @type {Array.<string>} */ | ||
var tokens = [] | ||
var input = String(value || '') | ||
var index = input.indexOf(',') | ||
var start = 0 | ||
/** @type {Array<string>} */ | ||
const tokens = [] | ||
const input = String(value || '') | ||
let index = input.indexOf(',') | ||
let start = 0 | ||
/** @type {boolean} */ | ||
var end | ||
/** @type {string} */ | ||
var token | ||
let end = false | ||
@@ -30,3 +38,3 @@ while (!end) { | ||
token = input.slice(start, index).trim() | ||
const token = input.slice(start, index).trim() | ||
@@ -45,17 +53,18 @@ if (token || !end) { | ||
/** | ||
* Serialize an array of strings to comma separated tokens. | ||
* Serialize an array of strings or numbers to comma-separated tokens. | ||
* | ||
* @param {Array.<string|number>} values | ||
* @param {StringifyOptions} [options] | ||
* @param {Array<string|number>} values | ||
* List of tokens. | ||
* @param {Options} [options] | ||
* Configuration for `stringify` (optional). | ||
* @returns {string} | ||
* Comma-separated tokens. | ||
*/ | ||
export function stringify(values, options) { | ||
var settings = options || {} | ||
const settings = options || {} | ||
// Ensure the last empty entry is seen. | ||
if (values[values.length - 1] === '') { | ||
values = values.concat('') | ||
} | ||
const input = values[values.length - 1] === '' ? [...values, ''] : values | ||
return values | ||
return input | ||
.join( | ||
@@ -62,0 +71,0 @@ (settings.padRight ? ' ' : '') + |
{ | ||
"name": "comma-separated-tokens", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"description": "Parse and stringify comma-separated tokens", | ||
@@ -34,19 +34,17 @@ "license": "MIT", | ||
"devDependencies": { | ||
"@types/tape": "^4.0.0", | ||
"@types/node": "^18.0.0", | ||
"c8": "^7.0.0", | ||
"prettier": "^2.0.0", | ||
"remark-cli": "^9.0.0", | ||
"remark-preset-wooorm": "^8.0.0", | ||
"rimraf": "^3.0.0", | ||
"tape": "^5.0.0", | ||
"remark-cli": "^11.0.0", | ||
"remark-preset-wooorm": "^9.0.0", | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^4.0.0", | ||
"xo": "^0.39.0" | ||
"xo": "^0.52.0" | ||
}, | ||
"scripts": { | ||
"prepack": "npm run build && npm run format", | ||
"build": "rimraf \"*.d.ts\" && tsc && type-coverage", | ||
"build": "tsc --build --clean && tsc --build && type-coverage", | ||
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", | ||
"test-api": "node test.js", | ||
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js", | ||
"test-api": "node --conditions development test.js", | ||
"test-coverage": "c8 --check-coverage --100 --reporter lcov npm run test-api", | ||
"test": "npm run build && npm run format && npm run test-coverage" | ||
@@ -63,7 +61,3 @@ }, | ||
"xo": { | ||
"prettier": true, | ||
"rules": { | ||
"no-var": "off", | ||
"prefer-arrow-callback": "off" | ||
} | ||
"prettier": true | ||
}, | ||
@@ -70,0 +64,0 @@ "remarkConfig": { |
104
readme.md
@@ -8,11 +8,36 @@ # comma-separated-tokens | ||
Parse and stringify comma separated tokens according to the [spec][]. | ||
Parse and stringify comma-separated tokens. | ||
## Contents | ||
* [What is this?](#what-is-this) | ||
* [When should I use this?](#when-should-i-use-this) | ||
* [Install](#install) | ||
* [Use](#use) | ||
* [API](#api) | ||
* [`parse(value)`](#parsevalue) | ||
* [`stringify(values[, options])`](#stringifyvalues-options) | ||
* [Types](#types) | ||
* [Compatibility](#compatibility) | ||
* [Related](#related) | ||
* [Contribute](#contribute) | ||
* [Security](#security) | ||
* [License](#license) | ||
## What is this? | ||
This is a tiny package that can parse and stringify comma-separated tokens, as | ||
used for example in the HTML `accept` attribute, according to the | ||
[WHATWG spec][spec]. | ||
## When should I use this? | ||
This package is rather niche, it’s low-level and particularly useful when | ||
working with [hast][]. | ||
## Install | ||
This package is ESM only: Node 12+ is needed to use it and it must be `import`ed | ||
instead of `require`d. | ||
This package is [ESM only][esm]. | ||
In Node.js (version 14.14+, 16.0+), install with [npm][]: | ||
[npm][]: | ||
```sh | ||
@@ -22,2 +47,16 @@ npm install comma-separated-tokens | ||
In Deno with [`esm.sh`][esmsh]: | ||
```js | ||
import {parse, stringify} from 'https://esm.sh/comma-separated-tokens@2' | ||
``` | ||
In browsers with [`esm.sh`][esmsh]: | ||
```html | ||
<script type="module"> | ||
import {parse, stringify} from 'https://esm.sh/comma-separated-tokens@2?bundle' | ||
</script> | ||
``` | ||
## Use | ||
@@ -34,3 +73,3 @@ | ||
This package exports the following identifiers: `parse`, `stringify`. | ||
This package exports the identifier `parse` and `stringify`. | ||
There is no default export. | ||
@@ -40,13 +79,18 @@ | ||
Parse comma separated tokens (`string`) to an array of strings, according | ||
to the [spec][]. | ||
Parse commma-separated tokens (`string`) to an array of strings | ||
(`Array<string>`), according to the [WHATWG spec][spec]. | ||
### `stringify(values[, options])` | ||
Compile an array of strings to comma separated tokens (`string`). | ||
Serialize an array of strings or numbers (`Array<string|number>`) to | ||
comma-separated tokens (`string`). | ||
Handles empty items at start or end correctly. | ||
Note that it’s not possible to specify initial or final whitespace per value. | ||
> 👉 **Note**: it’s not possible to specify initial or final whitespace per | ||
> value. | ||
##### `options` | ||
Configuration (optional). | ||
###### `options.padLeft` | ||
@@ -60,11 +104,31 @@ | ||
## Types | ||
This package is fully typed with [TypeScript][]. | ||
It exports the additional type `Options`. | ||
## Compatibility | ||
This package is at least compatible with all maintained versions of Node.js. | ||
As of now, that is Node.js 14.14+ and 16.0+. | ||
It also works in Deno and modern browsers. | ||
## Related | ||
* [`space-separated-tokens`](https://github.com/wooorm/space-separated-tokens) | ||
— parse/stringify space-separated tokens | ||
* [`collapse-white-space`](https://github.com/wooorm/collapse-white-space) | ||
— Replace multiple white-space characters with a single space | ||
— replace multiple white-space characters with a single space | ||
* [`property-information`](https://github.com/wooorm/property-information) | ||
— Information on HTML properties | ||
* [`space-separated-tokens`](https://github.com/wooorm/space-separated-tokens) | ||
— Parse/stringify space-separated tokens | ||
— info on HTML properties | ||
## Contribute | ||
Yes please! | ||
See [How to Contribute to Open Source][contribute]. | ||
## Security | ||
This package is safe. | ||
## License | ||
@@ -94,2 +158,10 @@ | ||
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c | ||
[esmsh]: https://esm.sh | ||
[typescript]: https://www.typescriptlang.org | ||
[contribute]: https://opensource.guide/how-to-contribute/ | ||
[license]: license | ||
@@ -99,2 +171,4 @@ | ||
[spec]: https://html.spec.whatwg.org/#comma-separated-tokens | ||
[spec]: https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#comma-separated-tokens | ||
[hast]: https://github.com/syntax-tree/hast |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9971
8
116
168