comma-separated-tokens
Advanced tools
Comparing version 1.0.8 to 2.0.0
75
index.js
@@ -1,19 +0,24 @@ | ||
'use strict' | ||
/** | ||
* @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`). | ||
*/ | ||
exports.parse = parse | ||
exports.stringify = stringify | ||
/** | ||
* Parse comma separated tokens to an array. | ||
* | ||
* @param {string} value | ||
* @returns {Array.<string>} | ||
*/ | ||
export function parse(value) { | ||
/** @type {Array.<string>} */ | ||
var tokens = [] | ||
var input = String(value || '') | ||
var index = input.indexOf(',') | ||
var start = 0 | ||
/** @type {boolean} */ | ||
var end | ||
/** @type {string} */ | ||
var token | ||
var comma = ',' | ||
var space = ' ' | ||
var empty = '' | ||
// Parse comma-separated tokens to an array. | ||
function parse(value) { | ||
var values = [] | ||
var input = String(value || empty) | ||
var index = input.indexOf(comma) | ||
var lastIndex = 0 | ||
var end = false | ||
var val | ||
while (!end) { | ||
@@ -25,29 +30,37 @@ if (index === -1) { | ||
val = input.slice(lastIndex, index).trim() | ||
token = input.slice(start, index).trim() | ||
if (val || !end) { | ||
values.push(val) | ||
if (token || !end) { | ||
tokens.push(token) | ||
} | ||
lastIndex = index + 1 | ||
index = input.indexOf(comma, lastIndex) | ||
start = index + 1 | ||
index = input.indexOf(',', start) | ||
} | ||
return values | ||
return tokens | ||
} | ||
// Compile an array to comma-separated tokens. | ||
// `options.padLeft` (default: `true`) pads a space left of each token, and | ||
// `options.padRight` (default: `false`) pads a space to the right of each token. | ||
function stringify(values, options) { | ||
/** | ||
* Serialize an array of strings to comma separated tokens. | ||
* | ||
* @param {Array.<string>} values | ||
* @param {StringifyOptions} [options] | ||
* @returns {string} | ||
*/ | ||
export function stringify(values, options) { | ||
var settings = options || {} | ||
var left = settings.padLeft === false ? empty : space | ||
var right = settings.padRight ? space : empty | ||
// Ensure the last empty entry is seen. | ||
if (values[values.length - 1] === empty) { | ||
values = values.concat(empty) | ||
if (values[values.length - 1] === '') { | ||
values = values.concat('') | ||
} | ||
return values.join(right + comma + left).trim() | ||
return values | ||
.join( | ||
(settings.padRight ? ' ' : '') + | ||
',' + | ||
(settings.padLeft === false ? '' : ' ') | ||
) | ||
.trim() | ||
} |
{ | ||
"name": "comma-separated-tokens", | ||
"version": "1.0.8", | ||
"version": "2.0.0", | ||
"description": "Parse and stringify comma-separated tokens", | ||
@@ -25,2 +25,6 @@ "license": "MIT", | ||
], | ||
"sideEffects": false, | ||
"type": "module", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"files": [ | ||
@@ -30,19 +34,20 @@ "index.js" | ||
"devDependencies": { | ||
"browserify": "^16.0.0", | ||
"nyc": "^15.0.0", | ||
"prettier": "^1.0.0", | ||
"remark-cli": "^7.0.0", | ||
"remark-preset-wooorm": "^6.0.0", | ||
"tape": "^4.0.0", | ||
"tinyify": "^2.0.0", | ||
"xo": "^0.25.0" | ||
"@types/tape": "^4.13.0", | ||
"c8": "^7.0.0", | ||
"prettier": "^2.0.0", | ||
"remark-cli": "^9.0.0", | ||
"remark-preset-wooorm": "^8.0.0", | ||
"rimraf": "^3.0.2", | ||
"tape": "^5.0.0", | ||
"type-coverage": "^2.17.0", | ||
"typescript": "^4.2.3", | ||
"xo": "^0.38.0" | ||
}, | ||
"scripts": { | ||
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix", | ||
"build-bundle": "browserify . -s commaSeparatedTokens -o comma-separated-tokens.js", | ||
"build-mangle": "browserify . -s commaSeparatedTokens -p tinyify -o comma-separated-tokens.min.js", | ||
"build": "npm run build-bundle && npm run build-mangle", | ||
"test-api": "node test", | ||
"test-coverage": "nyc --reporter lcov tape test.js", | ||
"test": "npm run format && npm run build && npm run test-coverage" | ||
"prepack": "npm run build && npm run format", | ||
"build": "rimraf \"*.d.ts\" && tsc && 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": "npm run build && npm run format && npm run test-coverage" | ||
}, | ||
@@ -59,6 +64,6 @@ "prettier": { | ||
"prettier": true, | ||
"esnext": false, | ||
"ignores": [ | ||
"comma-separated-tokens.js" | ||
] | ||
"rules": { | ||
"no-var": "off", | ||
"prefer-arrow-callback": "off" | ||
} | ||
}, | ||
@@ -69,3 +74,8 @@ "remarkConfig": { | ||
] | ||
}, | ||
"typeCoverage": { | ||
"atLeast": 100, | ||
"detail": true, | ||
"strict": true | ||
} | ||
} |
@@ -8,6 +8,9 @@ # comma-separated-tokens | ||
Parse and stringify comma-separated tokens according to the [spec][]. | ||
Parse and stringify comma separated tokens according to the [spec][]. | ||
## Install | ||
This package is ESM only: Node 12+ is needed to use it and it must be `import`ed | ||
instead of `require`d. | ||
[npm][]: | ||
@@ -22,6 +25,6 @@ | ||
```js | ||
var commaSeparated = require('comma-separated-tokens') | ||
import {parse, stringify} from 'comma-separated-tokens' | ||
commaSeparated.parse(' a ,b,,d d ') //=> ['a', 'b', '', 'd d'] | ||
commaSeparated.stringify(['a', 'b', '', 'd d']) //=> 'a, b, , d d' | ||
parse(' a ,b,,d d ') //=> ['a', 'b', '', 'd d'] | ||
stringify(['a', 'b', '', 'd d']) //=> 'a, b, , d d' | ||
``` | ||
@@ -31,10 +34,13 @@ | ||
### `commaSeparated.parse(value)` | ||
This package exports the following identifiers: `parse`, `stringify`. | ||
There is no default export. | ||
Parse comma-separated tokens (`string`) to an array of strings, according | ||
### `parse(value)` | ||
Parse comma separated tokens (`string`) to an array of strings, according | ||
to the [spec][]. | ||
### `commaSeparated.stringify(values[, options])` | ||
### `stringify(values[, options])` | ||
Compile an array of strings to comma-separated tokens (`string`). | ||
Compile an array of strings to comma separated tokens (`string`). | ||
Handles empty items at start or end correctly. | ||
@@ -68,5 +74,5 @@ Note that it’s not possible to specify initial or final whitespace per value. | ||
[build-badge]: https://img.shields.io/travis/wooorm/comma-separated-tokens.svg | ||
[build-badge]: https://github.com/wooorm/comma-separated-tokens/workflows/main/badge.svg | ||
[build]: https://travis-ci.org/wooorm/comma-separated-tokens | ||
[build]: https://github.com/wooorm/comma-separated-tokens/actions | ||
@@ -73,0 +79,0 @@ [coverage-badge]: https://img.shields.io/codecov/c/github/wooorm/comma-separated-tokens.svg |
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
6913
56
94
Yes
10