Skip to content

Instantly share code, notes, and snippets.

@sushant-at-nitor
Last active August 19, 2022 14:19
Show Gist options
  • Save sushant-at-nitor/b58c2baf7d589b6468bc261f52d408c9 to your computer and use it in GitHub Desktop.
Save sushant-at-nitor/b58c2baf7d589b6468bc261f52d408c9 to your computer and use it in GitHub Desktop.
tsv-to-json, tsv2json, tsvtojson in javascript / typescript, nodejs, node
const tsv = `
@PageTotal:120 @PageNow:1 @Num:599 @NumReturn:5
ID Name Image Countries Domains CommissionRate Category
39 Mothercare http://my-rewards.com/img/adv_logo/id-12639481.png "in" "mothercare.in" 4.41%~10.00% "Children","Costumes","Green & Eco","Menswear","Womenswear","Fashion","Activewear","Lingerie","Men","Women","Other","Shopping"
168 Droom http://my-rewards.com/img/adv_logo/in-867168490.jpeg "in" "droom.in" other "Travel","Travel"
169 IGP.com http://my-rewards.com/img/adv_logo/igp.png "global" "indiangiftsportal.com" 4.55% "Travel","Transportation"
211 Acer http://my-rewards.com/img/adv_logo/us-220211666.png "in" "store.acer.com" 2.45%~3.00% "Accessories","Computers","Computers","Hardware","Electronics"
865 Domino's Pizza http://my-rewards.com/img/adv_logo/us-535865648.jpeg "in" "pizzaonline.dominos.co.in","m.dominos.co.in" INR20.00 "Food & Drink","Restaurant","Gourmet","Food & Drink"
`;
tsvJSON(
tsv,
1, // skip [0] row
[
'brandId',
'name',
'image',
'countries',
'domains',
'commission',
'categories',
],
{
ID: (value) => Number(value),
Countries: (value) =>
(value?.split(',') || []).map((v) => v?.replace(/"/g, '')),
Domains: (value) =>
(value?.split(',') || []).map((v) => v?.replace(/"/g, '')),
Category: (value) =>
(value?.split(',') || []).map((v) => v?.replace(/"/g, '')),
},
),
);
export const tsvJSON = (
tsv: string,
headerLineIndex = 1,
customKeys = [],
customParsers = {},
) => {
if (!tsv || tsv === '') return [{}];
const lines = tsv.split('\n');
const items = [];
const headers = lines[headerLineIndex].split('\t');
for (let iLine = headerLineIndex + 1; iLine < lines.length; iLine++) {
const item = {};
const currentLine = lines[iLine].split('\t');
for (let iHeader = 0; iHeader < headers.length; iHeader++) {
item[
customKeys && customKeys.length > 0
? customKeys[iHeader]
: headers[iHeader]
] =
customParsers &&
customParsers[headers[iHeader]]
? customParsers[headers[iHeader]](currentLine[iHeader])
: currentLine[iHeader];
}
items.push(item);
}
return items;
};
// {ID: (value) => Number(value)}
type valueParserType = { [field: string]: (value: any) => any };
export const tsvJSON = (
tsv: string,
headerLineIndex = 1,
customKeys: string[] = [],
customParsers: valueParserType = {},
) => {
if (typeof tsv !== 'string')
throw new TypeError(`Expected string, got ${typeof tsv}`);
if (!tsv || tsv === '') return [{}];
const lines = tsv.split('\n');
const items = [];
const headers = lines[headerLineIndex].split('\t');
for (let iLine = headerLineIndex + 1; iLine < lines.length; iLine++) {
const item = {};
const currentLine = lines[iLine].split('\t');
for (let iHeader = 0; iHeader < headers.length; iHeader++) {
item[
customKeys && customKeys.length > 0
? customKeys[iHeader]
: headers[iHeader]
] =
customParsers &&
customParsers[headers[iHeader]]
? customParsers[headers[iHeader]](currentLine[iHeader])
: currentLine[iHeader];
}
items.push(item);
}
return items;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment