@nodescript/stdlib
Version:
Standard Node Definitions
36 lines (34 loc) • 1.02 kB
JavaScript
export const module = {
version: '1.1.4',
moduleName: 'String / Pad',
description: `
Adds padding either to the start or to the end of the specified string
(multiple times, if needed) until its length matches or exceeds the target length.
Does nothing if the string is already matches or exceeds the target length,
or if the padding is an empty string.`,
keywords: ['fill'],
params: {
string: {
schema: { type: 'string' },
},
length: {
schema: { type: 'number' },
},
padding: {
schema: { type: 'string', default: '0' },
},
start: {
schema: { type: 'boolean' },
}
},
result: {
schema: { type: 'string' },
}
};
export const compute = params => {
const { string, length, padding, start } = params;
if (!padding) {
return string;
}
return start ? string.padStart(length, padding) : string.padEnd(length, padding);
};