source: node_modules/comma-separated-tokens/index.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 1.1 KB
RevLine 
[d24f17c]1'use strict'
2
3exports.parse = parse
4exports.stringify = stringify
5
6var comma = ','
7var space = ' '
8var empty = ''
9
10// Parse comma-separated tokens to an array.
11function parse(value) {
12 var values = []
13 var input = String(value || empty)
14 var index = input.indexOf(comma)
15 var lastIndex = 0
16 var end = false
17 var val
18
19 while (!end) {
20 if (index === -1) {
21 index = input.length
22 end = true
23 }
24
25 val = input.slice(lastIndex, index).trim()
26
27 if (val || !end) {
28 values.push(val)
29 }
30
31 lastIndex = index + 1
32 index = input.indexOf(comma, lastIndex)
33 }
34
35 return values
36}
37
38// Compile an array to comma-separated tokens.
39// `options.padLeft` (default: `true`) pads a space left of each token, and
40// `options.padRight` (default: `false`) pads a space to the right of each token.
41function stringify(values, options) {
42 var settings = options || {}
43 var left = settings.padLeft === false ? empty : space
44 var right = settings.padRight ? space : empty
45
46 // Ensure the last empty entry is seen.
47 if (values[values.length - 1] === empty) {
48 values = values.concat(empty)
49 }
50
51 return values.join(right + comma + left).trim()
52}
Note: See TracBrowser for help on using the repository browser.