Skip to content

Commit

Permalink
Updated truncate() to handle array of strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Huggins committed Oct 22, 2019
1 parent a33fea9 commit 19e7dfd
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/TransactionMapping.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,4 @@ The syntax for mapping is based on object properties. The object to map from is
|**map**|any[], string|`macro['map'](input['someArrayOfObjects'], 'someProperty')['val']`|Method for returning an array of a specific property in array of objects.|
|**sum**|any[], string, [number=0]|`macro['sum'](input['someArrayOfObjects'], 'someProperty')['val']`|Method for returning the sum of an array of numbers, with an optional decimal places parameter.|
|**random**|N/A|`macro['random']()['val']`|Method for returning a random 4 digit number.|
|**truncate**|N/A|`macro['truncate']("testing", 4)['val']`|Method for truncating a string to the desired length.|
|**truncate**|string \| string[], number|`macro['truncate']("testing", 4)['val']`|Method for truncating a string or array of strings to the desired character length.|
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-x12",
"version": "1.3.4",
"version": "1.3.5",
"description": "ASC X12 parser, generator, query engine, and mapper; now with support for streams.",
"main": "index.js",
"keywords": [
Expand Down
10 changes: 8 additions & 2 deletions src/X12TransactionMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,15 @@ export class X12TransactionMap {
val: Math.floor(Math.random() * 10000)
}
},
truncate: function truncate (value: string, maxChars: number) {
truncate: function truncate (value: string|string[], maxChars: number) {
if (Array.isArray(value)) {
value = value.map((str) => str.substring(0, maxChars))
} else {
value = `${value}`.substring(0, maxChars)
}

return {
val: `${value}`.substring(0, maxChars)
val: value
}
}
}
Expand Down

0 comments on commit 19e7dfd

Please sign in to comment.