Skip to content

Commit

Permalink
Moved verifyStrings() to utils.js.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhop committed Aug 27, 2024
1 parent f75bcd4 commit 38978b6
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 60 deletions.
40 changes: 22 additions & 18 deletions src/bq-shell.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
'use strict';

const verifyStrings = strings => Array.isArray(strings) && strings.every(s => typeof s === 'string');
import {verifyStrings} from './utils.js';

const impl = (shellEscape, shell, options) => (strings, ...args) => {
const result = [];
const impl =
(shellEscape, shell, options) =>
(strings, ...args) => {
const result = [];

for (let i = 0; i < strings.length; i++) {
// process a string
result.push(strings[i]);
for (let i = 0; i < strings.length; i++) {
// process a string
result.push(strings[i]);

// process an argument
if (i >= args.length) continue;
const arg = String(args[i]);
if (!arg) continue;
result.push(shellEscape(arg));
}
// process an argument
if (i >= args.length) continue;
const arg = String(args[i]);
if (!arg) continue;
result.push(shellEscape(arg));
}

return shell(result.join(''), options);
};
return shell(result.join(''), options);
};

const bqShell = (shellEscape, shell) => (strings, ...args) => {
if (verifyStrings(strings)) return impl(shellEscape, shell, {})(strings, ...args);
return impl(shellEscape, shell, strings);
};
const bqShell =
(shellEscape, shell) =>
(strings, ...args) => {
if (verifyStrings(strings)) return impl(shellEscape, shell, {})(strings, ...args);
return impl(shellEscape, shell, strings);
};

export default bqShell;
88 changes: 46 additions & 42 deletions src/bq-spawn.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,65 @@
'use strict';

const verifyStrings = strings => Array.isArray(strings) && strings.every(s => typeof s === 'string');
import {verifyStrings} from './utils.js';

const impl = (spawn, options) => (strings, ...args) => {
const result = [];
const impl =
(spawn, options) =>
(strings, ...args) => {
const result = [];

let previousSpace = true;
for (let i = 0; i < strings.length; i++) {
// process a string
let previousSpace = true;
for (let i = 0; i < strings.length; i++) {
// process a string

let string = strings[i];
previousSpace ||= /^\s/.test(string);
if (previousSpace) string = string.trimStart();
const lastSpace = /\s$/.test(string);
if (lastSpace) string = string.trimEnd();
let string = strings[i];
previousSpace ||= /^\s/.test(string);
if (previousSpace) string = string.trimStart();
const lastSpace = /\s$/.test(string);
if (lastSpace) string = string.trimEnd();

let parts = string.split(/\s+/g).filter(part => part);
if (parts.length) {
if (!previousSpace) {
if (result.length) {
result[result.length - 1] += parts[0];
} else {
result.push(parts[0]);
let parts = string.split(/\s+/g).filter(part => part);
if (parts.length) {
if (!previousSpace) {
if (result.length) {
result[result.length - 1] += parts[0];
} else {
result.push(parts[0]);
}
parts = parts.slice(1);
}
parts = parts.slice(1);
result.push(...parts);
previousSpace = lastSpace;
} else {
previousSpace ||= lastSpace;
}
result.push(...parts);
previousSpace = lastSpace;
} else {
previousSpace ||= lastSpace;
}

// process an argument
// process an argument

if (i >= args.length) continue;
if (i >= args.length) continue;

const arg = String(args[i]);
if (!arg) continue;
const arg = String(args[i]);
if (!arg) continue;

if (previousSpace) {
result.push(arg);
} else {
if (result.length) {
result[result.length - 1] += arg;
} else {
if (previousSpace) {
result.push(arg);
} else {
if (result.length) {
result[result.length - 1] += arg;
} else {
result.push(arg);
}
}
previousSpace = false;
}
previousSpace = false;
}

return spawn(result, options);
};
return spawn(result, options);
};

const bqSpawn = spawn => (strings, ...args) => {
if (verifyStrings(strings)) return impl(spawn, {})(strings, ...args);
return impl(spawn, strings);
};
const bqSpawn =
spawn =>
(strings, ...args) => {
if (verifyStrings(strings)) return impl(spawn, {})(strings, ...args);
return impl(spawn, strings);
};

export default bqSpawn;
2 changes: 2 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ if (typeof Deno !== 'undefined') {
isAndroid = process.platform === 'android';
}
export {getEnv, isWindows, isAndroid};

export const verifyStrings = strings => Array.isArray(strings) && strings.every(s => typeof s === 'string');

0 comments on commit 38978b6

Please sign in to comment.