Socket
Socket
Sign inDemoInstall

nested_json_to_csv

Package Overview
Dependencies
66
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.0.2 to 2.0.3

build/Data/cartEntries.json

2

build/interfaces/IParser.d.ts
export interface IParser {
parse(...args: any): any;
get(): any;
}

@@ -7,2 +8,3 @@ export declare type ParserOptions = {

hasId?: boolean;
setDefaultValues?: boolean;
};

2

build/providers/factories/ParserFactory.d.ts

@@ -5,3 +5,3 @@ import { IFactory } from "../../interfaces/IFactory";

export declare class ParserFactory implements IFactory {
make(className: string, ...args: any[]): CsvParserService | JsonParserService | undefined;
make(className: string, ...args: any[]): JsonParserService | CsvParserService | undefined;
}

@@ -7,2 +7,3 @@ import { IParser } from "../../interfaces/IParser";

parse(): any;
get(): Csv;
}

@@ -28,2 +28,5 @@ "use strict";

}
get() {
return __classPrivateFieldGet(this, _CsvParserService_csv, "f");
}
}

@@ -30,0 +33,0 @@ exports.CsvParserService = CsvParserService;

@@ -7,3 +7,3 @@ import { IParser, ParserOptions } from "../../interfaces/IParser";

parse(headers: string[]): Csv;
getData(): Map<any, any>;
get(): Map<any, any>;
}

@@ -43,3 +43,3 @@ "use strict";

}
getData() {
get() {
return __classPrivateFieldGet(this, _JsonParserService_data, "f");

@@ -92,3 +92,7 @@ }

if (!data.has(path)) {
throw Error("Key" + path + " was not found in your JSON " + data);
if (__classPrivateFieldGet(this, _JsonParserService_parserOptions, "f").setDefaultValues) {
return "";
}
console.error(path, data);
throw Error("Key was not found in your JSON");
}

@@ -95,0 +99,0 @@ const value = data.get(path);

import { IFactory } from "../interfaces/IFactory";
import { IParser } from "../interfaces/IParser";
import { IParser, ParserOptions } from "../interfaces/IParser";
import { Csv } from "../models/Csv";
import { ParserFactory } from "../providers/factories/ParserFactory";
import { FileSystemService } from "../providers/services/FileSystemService";
export { ParserFactory, Csv, IParser, IFactory, FileSystemService };
export { ParserFactory, Csv, IParser, IFactory, FileSystemService, ParserOptions, };
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });

@@ -10,37 +19,35 @@ exports.FileSystemService = exports.Csv = exports.ParserFactory = void 0;

Object.defineProperty(exports, "FileSystemService", { enumerable: true, get: function () { return FileSystemService_1.FileSystemService; } });
// async function main() {
// const jsonFile = {
// "1": { person: { name: "fuad", id: "1443" }, price: 123 },
// "2": { person: { name: "test", id: "51542" }, price: 0 },
// };
// console.log(jsonFile);
// const parserFactory: IFactory = new ParserFactory();
// const jsonParser: IParser = parserFactory.make("jsonParser", jsonFile, {
// hasId: true,
// single: false,
// });
// const csv: Csv = jsonParser.parse(["person.id", "person.name", "price"]);
// const csvData: string = csv.make();
// console.log(csvData);
// const fileSystemService: FileSystemService = new FileSystemService(
// "data.csv",
// "./Data"
// );
// await fileSystemService.write(csvData);
// const csvParser: IParser = parserFactory.make("csvParser", csv);
// const json = csvParser.parse();
// console.log(json);
// const newJsonParser = parserFactory.make("jsonParser", json, {
// hasId: true,
// single: false,
// });
// const newCsv: Csv = newJsonParser.parse([
// "person.id",
// "person.name",
// "price",
// ]);
// console.log(newCsv.make());
// }
// main().then(() => {
// console.log("done");
// });
function main() {
return __awaiter(this, void 0, void 0, function* () {
const jsonFile = {
"1": { person: { name: "fuad", id: "1443" }, price: 123 },
"2": { person: { name: "test", id: "51542" } },
};
console.log(jsonFile);
const parserFactory = new ParserFactory_1.ParserFactory();
const options = {
hasId: true,
single: false,
setDefaultValues: true,
};
const jsonParser = parserFactory.make("jsonParser", jsonFile, options);
const csv = jsonParser.parse(["person.id", "person.name", "price"]);
const csvData = csv.make();
console.log(csvData);
const fileSystemService = new FileSystemService_1.FileSystemService("data.csv", "./Data");
yield fileSystemService.write(csvData);
const csvParser = parserFactory.make("csvParser", csv);
const json = csvParser.parse();
console.log(json);
const newJsonParser = parserFactory.make("jsonParser", json, options);
const newCsv = newJsonParser.parse([
"person.id",
"person.name",
"price",
]);
console.log(newCsv.make());
});
}
main().then(() => {
console.log("done");
});

@@ -11,21 +11,2 @@ "use strict";

describe("FileIO test", () => {
// test("simple single json to csv", () => {
// const json = {
// name: "fox",
// id: "31245324",
// };
// let parserFactory: IFactory = new ParserFactory();
// const parser: IParser = parserFactory.make("jsonParser", json, {
// hasId: false,
// single: true,
// });
// expect(parser).toBeInstanceOf(JsonParserService);
// const headers = ["id", "name"];
// const csv: Csv = parser.parse(headers);
// const data: string = csv.make();
// const rows = csv.makeRows().getRows();
// expect(rows).toEqual([["31245324", "fox"]]);
// expect(data).toEqual("id,name\n31245324,fox,");
// expect(csv.getHeaders()).toEqual(headers);
// });
test("Create a csv file", (done) => {

@@ -32,0 +13,0 @@ const json = {

@@ -9,2 +9,3 @@ export interface IParser {

hasId?: boolean;
setDefaultValues?: boolean;
};

@@ -5,3 +5,3 @@ /** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */

testEnvironment: 'node',
testPathIgnorePatterns: ['./node_modules/', './build'],
root: "tests",
};
{
"name": "nested_json_to_csv",
"version": "2.0.2",
"version": "2.0.3",
"description": "",

@@ -8,3 +8,3 @@ "main": "build/src/index.js",

"directories": {
"test": "test"
"test": "tests"
},

@@ -11,0 +11,0 @@ "prepublish": "tsc",

@@ -78,3 +78,7 @@ import { isBooleanObject, isNumberObject, isStringObject } from "util/types";

if (!data.has(path)) {
throw Error("Key" + path + " was not found in your JSON " + data);
if (this.#parserOptions.setDefaultValues) {
return "";
}
console.error(path, data);
throw Error("Key was not found in your JSON");
}

@@ -81,0 +85,0 @@ const value: any = data.get(path);

import { IFactory } from "../interfaces/IFactory";
import { IParser } from "../interfaces/IParser";
import { IParser, ParserOptions } from "../interfaces/IParser";
import { Csv } from "../models/Csv";

@@ -11,44 +11,47 @@ import { ParserFactory } from "../providers/factories/ParserFactory";

IFactory,
FileSystemService
}
// async function main() {
// const jsonFile = {
// "1": { person: { name: "fuad", id: "1443" }, price: 123 },
// "2": { person: { name: "test", id: "51542" }, price: 0 },
// };
FileSystemService,
ParserOptions,
};
async function main() {
const jsonFile = {
"1": { person: { name: "fuad", id: "1443" }, price: 123 },
"2": { person: { name: "test", id: "51542" } },
};
// console.log(jsonFile);
// const parserFactory: IFactory = new ParserFactory();
console.log(jsonFile);
const parserFactory: IFactory = new ParserFactory();
const options: ParserOptions = {
hasId: true,
single: false,
setDefaultValues: true,
};
const jsonParser: IParser = parserFactory.make(
"jsonParser",
jsonFile,
options
);
const csv: Csv = jsonParser.parse(["person.id", "person.name", "price"]);
const csvData: string = csv.make();
console.log(csvData);
// const jsonParser: IParser = parserFactory.make("jsonParser", jsonFile, {
// hasId: true,
// single: false,
// });
// const csv: Csv = jsonParser.parse(["person.id", "person.name", "price"]);
// const csvData: string = csv.make();
// console.log(csvData);
const fileSystemService: FileSystemService = new FileSystemService(
"data.csv",
"./Data"
);
await fileSystemService.write(csvData);
// const fileSystemService: FileSystemService = new FileSystemService(
// "data.csv",
// "./Data"
// );
// await fileSystemService.write(csvData);
const csvParser: IParser = parserFactory.make("csvParser", csv);
const json = csvParser.parse();
console.log(json);
const newJsonParser = parserFactory.make("jsonParser", json, options);
const newCsv: Csv = newJsonParser.parse([
"person.id",
"person.name",
"price",
]);
console.log(newCsv.make());
}
// const csvParser: IParser = parserFactory.make("csvParser", csv);
// const json = csvParser.parse();
// console.log(json);
// const newJsonParser = parserFactory.make("jsonParser", json, {
// hasId: true,
// single: false,
// });
// const newCsv: Csv = newJsonParser.parse([
// "person.id",
// "person.name",
// "price",
// ]);
// console.log(newCsv.make());
// }
// main().then(() => {
// console.log("done");
// });
main().then(() => {
console.log("done");
});

@@ -11,21 +11,2 @@ import { IFactory } from "../interfaces/IFactory";

describe("FileIO test", () => {
// test("simple single json to csv", () => {
// const json = {
// name: "fox",
// id: "31245324",
// };
// let parserFactory: IFactory = new ParserFactory();
// const parser: IParser = parserFactory.make("jsonParser", json, {
// hasId: false,
// single: true,
// });
// expect(parser).toBeInstanceOf(JsonParserService);
// const headers = ["id", "name"];
// const csv: Csv = parser.parse(headers);
// const data: string = csv.make();
// const rows = csv.makeRows().getRows();
// expect(rows).toEqual([["31245324", "fox"]]);
// expect(data).toEqual("id,name\n31245324,fox,");
// expect(csv.getHeaders()).toEqual(headers);
// });
test("Create a csv file", (done) => {

@@ -49,3 +30,3 @@ const json = {

fileSystemService.write(data).then(() => {
const exists:boolean = fs.existsSync(fileSystemService.getFullPath());
const exists: boolean = fs.existsSync(fileSystemService.getFullPath());
expect(exists).toEqual(true);

@@ -63,12 +44,11 @@ done();

const fileSystemService: FileSystemService = new FileSystemService(
"data.json",
"./Data"
);
"data.json",
"./Data"
);
fileSystemService.write(JSON.stringify(json)).then(() => {
const exists:boolean = fs.existsSync(fileSystemService.getFullPath());
expect(exists).toEqual(true);
done();
});
const exists: boolean = fs.existsSync(fileSystemService.getFullPath());
expect(exists).toEqual(true);
done();
});
});
});
import { IFactory } from "../interfaces/IFactory";
import { IParser } from "../interfaces/IParser";
import { IParser, ParserOptions } from "../interfaces/IParser";
import { Csv } from "../models/Csv";

@@ -72,2 +72,29 @@ import { ParserFactory } from "../providers/factories/ParserFactory";

});
test("complex nested json to csv with defualt values", () => {
const json = {
user: { name: "fox", uid: "31245324", address: { city: "nowhere" } },
child: { name: "fox", uid: "31245324" },
};
let parserFactory: IFactory = new ParserFactory();
const options: ParserOptions = {
hasId: true,
single: false,
setDefaultValues: true,
};
const parser: IParser = parserFactory.make("jsonParser", json, options);
expect(parser).toBeInstanceOf(JsonParserService);
const headers = ["uid", "name", "address.city"];
const csv: Csv = parser.parse(headers);
const data: string = csv.make();
const rows = csv.makeRows().getRows();
console.log(rows);
expect(rows).toEqual([
["user", "31245324", "fox", "nowhere"],
["child", "31245324", "fox"],
]);
expect(data).toEqual(
"id,uid,name,address.city\nuser,31245324,fox,nowhere,\nchild,31245324,fox,,\n"
);
expect(csv.getHeaders()).toEqual(headers);
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc