mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-10 01:43:43 +00:00
refactor: improve payload type for OSC (#954)
--------- Co-authored-by: Joel Wetzell <jwetzell@yahoo.com> Co-authored-by: arc-alex <ac@omnivox.dk>
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
import { splitWhitespace } from './splitWhitespace';
|
||||
|
||||
describe('test splitWhitespace() function', () => {
|
||||
it('empty string', () => {
|
||||
const test = '';
|
||||
expect(splitWhitespace(test)).toStrictEqual(null);
|
||||
});
|
||||
|
||||
it('just space', () => {
|
||||
const test = ' ';
|
||||
expect(splitWhitespace(test)).toStrictEqual(null);
|
||||
});
|
||||
|
||||
it('1 item', () => {
|
||||
const test = 'test';
|
||||
expect(splitWhitespace(test)).toStrictEqual(['test']);
|
||||
});
|
||||
|
||||
it('2 items', () => {
|
||||
const test = 'test test';
|
||||
expect(splitWhitespace(test)).toStrictEqual(['test', 'test']);
|
||||
});
|
||||
|
||||
it('2 items and quoted string', () => {
|
||||
const test = 'test test "more test"';
|
||||
expect(splitWhitespace(test)).toStrictEqual(['test', 'test', '"more test"']);
|
||||
});
|
||||
|
||||
it('2 sapces', () => {
|
||||
const test = 'test test "more test"';
|
||||
expect(splitWhitespace(test)).toStrictEqual(['test', 'test', '"more test"']);
|
||||
});
|
||||
|
||||
it('quotes without spaces', () => {
|
||||
const test = 'test test "moreTest"';
|
||||
expect(splitWhitespace(test)).toStrictEqual(['test', 'test', '"moreTest"']);
|
||||
});
|
||||
|
||||
it('escaped quotes', () => {
|
||||
const test = 'test test "more \\" test"';
|
||||
expect(splitWhitespace(test)).toStrictEqual(['test', 'test', '"more " test"']);
|
||||
});
|
||||
|
||||
it('missing end quotes', () => {
|
||||
const test = 'test test "more test';
|
||||
expect(splitWhitespace(test)).toStrictEqual(['test', 'test', '"more test']);
|
||||
});
|
||||
|
||||
it('missing start quotes', () => {
|
||||
const test = 'test test more test"';
|
||||
expect(splitWhitespace(test)).toStrictEqual(['test', 'test', 'more', 'test"']);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,37 @@
|
||||
const splitRegex = /\\?.|^$/g;
|
||||
|
||||
/**
|
||||
* adapted from {@link https://stackoverflow.com/questions/4031900/split-a-string-by-whitespace-keeping-quoted-segments-allowing-escaped-quotes this}
|
||||
* @param str string to split
|
||||
* @returns
|
||||
*/
|
||||
export const splitWhitespace = (str: string, keepQuotes = true): null | string[] => {
|
||||
const match = str.match(splitRegex);
|
||||
if (!match || match[0] == '') {
|
||||
return null;
|
||||
}
|
||||
const array = match
|
||||
.reduce(
|
||||
(accumulator, current) => {
|
||||
if (current === '"') {
|
||||
accumulator.inQuotes ^= 1;
|
||||
if (keepQuotes) {
|
||||
accumulator.array[accumulator.array.length - 1] += current.replace(/\\(.)/, '$1');
|
||||
}
|
||||
} else if (!accumulator.inQuotes && current === ' ') {
|
||||
accumulator.array.push('');
|
||||
} else {
|
||||
accumulator.array[accumulator.array.length - 1] += current.replace(/\\(.)/, '$1');
|
||||
}
|
||||
return accumulator;
|
||||
},
|
||||
{ array: [''], inQuotes: 0 },
|
||||
)
|
||||
.array.filter((value) => value != '');
|
||||
|
||||
if (!array.length) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return array;
|
||||
};
|
||||
Reference in New Issue
Block a user