Skip to content

Commit

Permalink
Merge branch 'tlwr-feature-select-lines-op'
Browse files Browse the repository at this point in the history
  • Loading branch information
n1474335 committed Apr 23, 2017
2 parents 07bb095 + 2c0f0d9 commit 05edc1f
Show file tree
Hide file tree
Showing 4 changed files with 309 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/core/config/Categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ const Categories = [
"Unique",
"Split",
"Filter",
"Head",
"Tail",
"Count occurrences",
"Expand alphabet range",
"Parse escaped string",
Expand Down
54 changes: 53 additions & 1 deletion src/core/config/OperationConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -3196,7 +3196,59 @@ const OperationConfig = {
outputType: "html",
args: [
]
}
},
"Head": {
description: [
"Like the UNIX head utility.",
"<br>",
"Gets the first n lines.",
"<br>",
"You can select all but the last n lines by entering a negative value for n.",
"<br>",
"The delimiter can be changed so that instead of lines, fields (i.e. commas) are selected instead.",
].join("\n"),
run: StrUtils.runHead,
inputType: "string",
outputType: "string",
args: [
{
name: "Delimiter",
type: "option",
value: StrUtils.DELIMITER_OPTIONS
},
{
name: "Number",
type: "number",
value: 10,
},
]
},
"Tail": {
description: [
"Like the UNIX tail utility.",
"<br>",
"Gets the last n lines.",
"<br>",
"Optionally you can select all lines after line n by entering a negative value for n.",
"<br>",
"The delimiter can be changed so that instead of lines, fields (i.e. commas) are selected instead.",
].join("\n"),
run: StrUtils.runTail,
inputType: "string",
outputType: "string",
args: [
{
name: "Delimiter",
type: "option",
value: StrUtils.DELIMITER_OPTIONS
},
{
name: "Number",
type: "number",
value: 10,
},
]
},
};

export default OperationConfig;
56 changes: 56 additions & 0 deletions src/core/operations/StrUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,62 @@ const StrUtils = {
},


/**
* Head lines operation.
*
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
runHead: function(input, args) {
let delimiter = args[0],
number = args[1];

delimiter = Utils.charRep[delimiter];
let splitInput = input.split(delimiter);

return splitInput
.filter((line, lineIndex) => {
lineIndex += 1;

if (number < 0) {
return lineIndex <= splitInput.length + number;
} else {
return lineIndex <= number;
}
})
.join(delimiter);
},


/**
* Tail lines operation.
*
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
runTail: function(input, args) {
let delimiter = args[0],
number = args[1];

delimiter = Utils.charRep[delimiter];
let splitInput = input.split(delimiter);

return splitInput
.filter((line, lineIndex) => {
lineIndex += 1;

if (number < 0) {
return lineIndex > -number;
} else {
return lineIndex > splitInput.length - number;
}
})
.join(delimiter);
},


/**
* Adds HTML highlights to matches within a string.
*
Expand Down
198 changes: 198 additions & 0 deletions test/tests/operations/StrUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,202 @@ TestRegister.addTests([
}
],
},
{
name: "Head 0",
input: [1, 2, 3, 4, 5, 6].join("\n"),
expectedOutput: [].join("\n"),
recipeConfig: [
{
"op": "Head",
"args": ["Line feed", 0]
}
],
},
{
name: "Head 1",
input: [1, 2, 3, 4, 5, 6].join("\n"),
expectedOutput: [1].join("\n"),
recipeConfig: [
{
"op": "Head",
"args": ["Line feed", 1]
}
],
},
{
name: "Head 2",
input: [1, 2, 3, 4, 5, 6].join("\n"),
expectedOutput: [1, 2].join("\n"),
recipeConfig: [
{
"op": "Head",
"args": ["Line feed", 2]
}
],
},
{
name: "Head 6",
input: [1, 2, 3, 4, 5, 6].join("\n"),
expectedOutput: [1, 2, 3, 4, 5, 6].join("\n"),
recipeConfig: [
{
"op": "Head",
"args": ["Line feed", 6]
}
],
},
{
name: "Head big",
input: [1, 2, 3, 4, 5, 6].join("\n"),
expectedOutput: [1, 2, 3, 4, 5, 6].join("\n"),
recipeConfig: [
{
"op": "Head",
"args": ["Line feed", 100]
}
],
},
{
name: "Head all but 1",
input: [1, 2, 3, 4, 5, 6].join("\n"),
expectedOutput: [1, 2, 3, 4, 5].join("\n"),
recipeConfig: [
{
"op": "Head",
"args": ["Line feed", -1]
}
],
},
{
name: "Head all but 2",
input: [1, 2, 3, 4, 5, 6].join("\n"),
expectedOutput: [1, 2, 3, 4].join("\n"),
recipeConfig: [
{
"op": "Head",
"args": ["Line feed", -2]
}
],
},
{
name: "Head all but 6",
input: [1, 2, 3, 4, 5, 6].join("\n"),
expectedOutput: [].join("\n"),
recipeConfig: [
{
"op": "Head",
"args": ["Line feed", -6]
}
],
},
{
name: "Head all but big",
input: [1, 2, 3, 4, 5, 6].join("\n"),
expectedOutput: [].join("\n"),
recipeConfig: [
{
"op": "Head",
"args": ["Line feed", -100]
}
],
},
{
name: "Tail 0",
input: [1, 2, 3, 4, 5, 6].join("\n"),
expectedOutput: [].join("\n"),
recipeConfig: [
{
"op": "Tail",
"args": ["Line feed", 0]
}
],
},
{
name: "Tail 1",
input: [1, 2, 3, 4, 5, 6].join("\n"),
expectedOutput: [6].join("\n"),
recipeConfig: [
{
"op": "Tail",
"args": ["Line feed", 1]
}
],
},
{
name: "Tail 2",
input: [1, 2, 3, 4, 5, 6].join("\n"),
expectedOutput: [5, 6].join("\n"),
recipeConfig: [
{
"op": "Tail",
"args": ["Line feed", 2]
}
],
},
{
name: "Tail 6",
input: [1, 2, 3, 4, 5, 6].join("\n"),
expectedOutput: [1, 2, 3, 4, 5, 6].join("\n"),
recipeConfig: [
{
"op": "Tail",
"args": ["Line feed", 6]
}
],
},
{
name: "Tail big",
input: [1, 2, 3, 4, 5, 6].join("\n"),
expectedOutput: [1, 2, 3, 4, 5, 6].join("\n"),
recipeConfig: [
{
"op": "Tail",
"args": ["Line feed", 100]
}
],
},
{
name: "Tail all but 1",
input: [1, 2, 3, 4, 5, 6].join("\n"),
expectedOutput: [2, 3, 4, 5, 6].join("\n"),
recipeConfig: [
{
"op": "Tail",
"args": ["Line feed", -1]
}
],
},
{
name: "Tail all but 2",
input: [1, 2, 3, 4, 5, 6].join("\n"),
expectedOutput: [3, 4, 5, 6].join("\n"),
recipeConfig: [
{
"op": "Tail",
"args": ["Line feed", -2]
}
],
},
{
name: "Tail all but 6",
input: [1, 2, 3, 4, 5, 6].join("\n"),
expectedOutput: [].join("\n"),
recipeConfig: [
{
"op": "Tail",
"args": ["Line feed", -6]
}
],
},
{
name: "Tail all but big",
input: [1, 2, 3, 4, 5, 6].join("\n"),
expectedOutput: [].join("\n"),
recipeConfig: [
{
"op": "Tail",
"args": ["Line feed", -100]
}
],
},
]);

0 comments on commit 05edc1f

Please sign in to comment.