-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
split_funcs.js
40 lines (34 loc) · 1.15 KB
/
split_funcs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
split the input text in to 'sections' according to field delimiter rules
eg:
foo-bar baz, bing-bong bop baz
|------||---||--------||--||---| tokens
|===========|==================| sections
**/
const whitespace = new RegExp(/^\s$/)
const quotes = `"«»‘’‚‛“”„‟‹›⹂「」『』〝〞〟﹁﹂﹃﹄"'「」`
function fieldsFuncBoundary (char) {
switch (char) {
case '\n':
return true
case '\t':
return true
case ',':
return true
default:
// @todo: this should ideally only work for 'matching pairs' of quotes
if (quotes.includes(char)) { return true }
return false
}
}
// test for any unicode whitespace char including newlines and tabs
// @todo: is this possible in js without using a regex?
function fieldsFuncWhiteSpace (char) {
return whitespace.test(char)
}
function fieldsFuncHyphenOrWhiteSpace (char) {
return char === '-' || char === '/' || fieldsFuncWhiteSpace(char)
}
module.exports.fieldsFuncBoundary = fieldsFuncBoundary
module.exports.fieldsFuncWhiteSpace = fieldsFuncWhiteSpace
module.exports.fieldsFuncHyphenOrWhiteSpace = fieldsFuncHyphenOrWhiteSpace