Socket
Socket
Sign inDemoInstall

nested_json_to_csv

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nested_json_to_csv - npm Package Compare versions

Comparing version 2.0.4 to 2.0.5

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[]): JsonParserService | CsvParserService | undefined;
make(className: string, ...args: any[]): CsvParserService | JsonParserService | undefined;
}

@@ -15,2 +15,3 @@ "use strict";

}
//TODO: add NULL desgin pattern here
}

@@ -17,0 +18,0 @@ catch (error) {

"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 });

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

Object.defineProperty(exports, "FileSystemService", { enumerable: true, get: function () { return FileSystemService_1.FileSystemService; } });
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");
});
// 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();
// 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 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());
// }
// main().then(() => {
// console.log("done");
// });

@@ -69,2 +69,27 @@ "use strict";

});
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 = new ParserFactory_1.ParserFactory();
const options = {
hasId: true,
single: false,
setDefaultValues: true,
};
const parser = parserFactory.make("jsonParser", json, options);
expect(parser).toBeInstanceOf(JsonParserService_1.JsonParserService);
const headers = ["uid", "name", "address.city"];
const csv = parser.parse(headers);
const data = 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);
});
});
{
"name": "nested_json_to_csv",
"version": "2.0.4",
"version": "2.0.5",
"description": "",

@@ -5,0 +5,0 @@ "main": "build/src/index.js",

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc