Skip to content

Commit

Permalink
feat: support date fields and [date] column flag
Browse files Browse the repository at this point in the history
  • Loading branch information
JaredReisinger committed Sep 14, 2024
1 parent 9ffce51 commit e00d6d7
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 63 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ The various `--omit...` and `--include` options control whether columns are incl

If the `--omit-...` options are too coarse, you can enable or disable individual columns using the `--include` and `--omit` options, or for complete control use the `--columns` option to specify _exactly_ what columns you want displayed and in what order they will appear. Using `--list-columns` will list all of the columns present in a given set of items. Do note that unless you use `--columns` to specify an exact set, the following columns are _always_ included: "order#", "date", "name", "email", "qty", and "total".

The `--columns` option also allows for columns/fields with commas in the name by backslash-escaping the comma, `as\,such`. You can also provide column aliases if you need to rename the output column using the `alias=real_name` syntax.
The `--columns` option also allows for columns/fields with commas in the name by backslash-escaping the comma, `as\,such`. You can also provide column aliases if you need to rename the output column using the `alias=real_name` syntax. Additionally, you can prefix a column/field with `[date]` to attempt to parse unix timestamps and format the values as dates.

### Examples

Expand Down
2 changes: 1 addition & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ async function loadConfig(): Promise<ConfigFile> {
`.${pkgInfo?.packageJson.name}.json`
);
// can't really use dbg() since this is called *before* parsing options!
// dbg(0, 'checking for config', { filename });
// dbg(1, 'checking for config', { filename });
try {
const data = await readFileAsync(filename);
const cfg = JSON.parse(data.toString()) as ConfigFile;
Expand Down
33 changes: 20 additions & 13 deletions src/commands/get.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,19 +140,12 @@ test('Get.createCommand("foo").builder supports after/before parsing', async (t)
}
});

const meta = {
allBlank: false,
allIdentical: false,
maximumWidth: 100,
};

test('Get.getValue() should extract a field', (t) => {
const get = createGet();
t.is(
get.getValue({ field: 'FIELD' } as unknown as WooItem, {
value: 'field',
label: '',
meta,
}),
'FIELD'
);
Expand All @@ -164,7 +157,6 @@ test('Get.getValue() should handle nested fields', (t) => {
get.getValue({ outer: { inner: 'FIELD' } } as unknown as WooItem, {
value: 'outer.inner',
label: '',
meta,
}),
'FIELD'
);
Expand All @@ -180,7 +172,6 @@ test('Get.getValue() should handle extractor functions', (t) => {
unknown
>,
label: '',
meta,
}),
'FN(FIELD)'
);
Expand Down Expand Up @@ -229,11 +220,18 @@ test('Get.collectMetaFields() should return meta field descriptors', (t) => {
]);
});

test('Get.formatDateTime()should return an Excel-formatted date/time', (t) => {
const get = createGet();
// use go-lang's magic date: "01/02 03:04:05PM ‘06 -0700" (minus TZ)
const result = get.formatDateTime(new Date(Date.UTC(2006, 0, 2, 3, 4, 5)));
t.is(result, '1/2/2006 3:04:05 AM');
});

test('Get.formatDate()should return an Excel-formatted date', (t) => {
const get = createGet();
// use go-lang's magic date: "01/02 03:04:05PM ‘06 -0700" (minus TZ)
const result = get.formatDate(new Date(Date.UTC(2006, 0, 2, 3, 4, 5)));
t.is(result, '1/2/2006 3:04:05 AM');
t.is(result, '1/2/2006');
});

test('Get.formatAmount() should return formatted amount', (t) => {
Expand Down Expand Up @@ -383,23 +381,32 @@ test('Get.run() can handle escaped-comma columns', async (t) => {
});

test('Get.createColumnFields() can handle list of columns', (t) => {
const actual = Get.createColumnFields('one,two,three', []);
const get = createGet();
const actual = get.createColumnFields('one,two,three', []);
t.like(actual[0], { label: 'one' });
t.like(actual[1], { label: 'two' });
t.like(actual[2], { label: 'three' });
});

test('Get.createColumnFields() can handle escaped-comma columns', (t) => {
const actual = Get.createColumnFields('one\\,two,three', []);
const get = createGet();
const actual = get.createColumnFields('one\\,two,three', []);
t.like(actual[0], { label: 'one,two' });
t.like(actual[1], { label: 'three' });
});

test('Get.createColumnFields() can handle column alias', (t) => {
const actual = Get.createColumnFields('the_sku=sku', []);
const get = createGet();
const actual = get.createColumnFields('the_sku=sku', []);
t.like(actual[0], { label: 'the_sku' });
});

test('Get.createColumnFields() can handle date-flagged field', (t) => {
const get = createGet();
const actual = get.createColumnFields('[date]name', []);
t.like(actual[0], { label: 'name' });
});

test('Get.run() can output CSV to a file', async (t) => {
const get = createGet();
const { client, stub } = createClientStub();
Expand Down
Loading

0 comments on commit e00d6d7

Please sign in to comment.