Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,12 @@ Type: `<Number>`

Field's width. Required.

#### `field.stringify`

Type: `<Function>`

A function that converts the field into a custom string. Useful for custom formatting, for example, formatting a Date into DD/MM/YYYY format. Only used while stringifying.

## Errors

All errors that can occur during the parsing or serializing phase contain an error code. Error objects also contain enough info (properties) to debug the problem.
Expand Down
6 changes: 6 additions & 0 deletions fixed-width.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ export interface Field {
value: string,
context: { column: number; line: number; width: number }
) => any;
/**
* Custom way of writing the value to the file
*/
stringify?: (
value: any
) => string;
/**
* Field's column number. This is 1-based. First column is 1.
*/
Expand Down
1 change: 1 addition & 0 deletions src/options.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ function parseField (field, index, defaultColumn, defaultPad, encoding) {
column,
pad,
property: isPropertyKey(field.property) ? field.property : index,
stringify: typeof field.stringify === 'function' ? field.stringify : null,
width: field.width
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/options.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ test('defaults', t => {
column: 1,
pad: ' ',
property: 0,
stringify: null,
width: 2
},
{
Expand All @@ -29,6 +30,7 @@ test('defaults', t => {
column: 3,
pad: ' ',
property: 1,
stringify: null,
width: 2
}
],
Expand Down
7 changes: 6 additions & 1 deletion src/stringify.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ export function replaceWith (text, value, index = 0) {
}

export function stringifyField (obj, field, options, line) {
let value = stringifyValue(obj[field.property], options.encoding)
let value = obj[field.property]
if (field.stringify) {
value = field.stringify(value)
}
value = stringifyValue(value, options.encoding)

if (typeof value !== 'string') {
throw new FixedWidthError(
'EXPECTED_STRING_VALUE',
Expand Down
25 changes: 25 additions & 0 deletions src/stringify.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,31 @@ test('stringify fields', t => {
t.is(text, ' 1.3 42 ')
})

test('stringify with custom stringifier', t => {
const options = parseOptions([
{
align: 'left',
property: 'b',
width: 10,
stringify: (date) => {
// Sample formatting that writes the date in American format (MM/DD/YYYY)
const year = (date.getYear() + 1900).toString()
const month = date.getMonth().toString().padStart(2, '0')
const day = date.getDate().toString().padStart(2, '0')
const result = `${month}/${day}/${year}`
return result
}
}
])

const values = {
b: new Date(2020, 3, 15)
}

const text = stringifyFields(values, options)
t.is(text, '03/15/2020')
})

test('expected string value', t => {
t.throws(
() => stringify(
Expand Down