Cut
Cut a string by delimiter and return specific fields. Example ================= input: "A-B-C-D-E" delimiter: "-" fields: "1,5" return: "A-E".
javascript · Filters And Transformers
Source
function parseValue(value) { /* This function is a workaround for an issue where passing a date field from the context to the "value" argument, in JS it is accepted as an object, while in python it is a string. To solve this discrapancy, we ensure the value is stringified correctly before running the cut() function. */ if (typeof value === "string") { return value; } else if (typeof value === "number") { return "" + value; } value = JSON.stringify(value); if (value.startsWith('"') && value.endsWith('"')) { // potentially a date object, wrapped with quotes value = value.slice(1, -1); try { Date(value); return value; } catch(e) { // unexpected value - will throw the error below } } // value type isn't supported for Cut (either a boolean or a dict) throw "Unsupported value for Cut: " + value; } function cut(value, fields, delim) { value = parseValue(value); if (delim === "''") { delim = ""; } const data = value.split(delim); fields = fields.split(",").map(num => parseInt(num, 10)); const maxIndex = Math.max(...fields); if (data.length < maxIndex) { throw new Error(`Invalid field index ${maxIndex}, should be between 1 to ${data.length}.`); } return fields.map(i => data[i - 1]).join(delim); } return cut(args.value, args.fields, args.delimiter);
README
Cuts a string by delimiter and returns specific fields.
Examples
input: “A-B-C-D-E”
delimiter: “-“
fields: “1,5”
return: “A-E”
Script Data
| Name | Description |
|---|---|
| Script Type | python |
| Tags | transformer, string |
Inputs
| Argument Name | Description |
|---|---|
| value | The value to split. |
| delimiter | The delimiter to cut the string by. Pass ‘’ to set delimiter to be empty string. |
| fields | The comma-separated field numbers. For example, “1,5,7”. |
Outputs
There are no outputs for this script.