Socket
Socket
Sign inDemoInstall

parse-csv-simple

Package Overview
Dependencies
36
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.2 to 1.0.3

0

.mocharc.json
{
"require": "ts-node/register"
}

30

dist/builders/iterator-builder.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.IteratorBuilder = void 0;
const utils_1 = require("../utils");
class IteratorBuilder {
constructor(option) {
this.next = null;
this.tempRow = [];
this.isEnd = false;
this.fetchedRows = 0;
this.limit = (option === null || option === void 0 ? void 0 : option.limit) || Infinity;
}
onEnd() {
this.isEnd = true;
}
onCell() {
onCell(cell) {
this.tempRow.push(cell);
}
onRowEnd() {
onRowEnd(next) {
this.fetchedRows++;
if (this.fetchedRows >= this.limit) {
this.isEnd = true;
}
this.next = next;
}
getResult() {
return [];
const self = this;
return {
*[Symbol.iterator]() {
while (!self.isEnd) {
yield self.tempRow.slice();
self.tempRow = [];
self.next && self.next();
}
if (!utils_1.isArrayEmpty(self.tempRow)) {
yield self.tempRow.slice();
}
}
};
}
}
exports.IteratorBuilder = IteratorBuilder;

@@ -0,0 +0,0 @@ "use strict";

@@ -0,0 +0,0 @@ "use strict";

28

dist/index.js

@@ -6,17 +6,27 @@ "use strict";

const sync_builder_1 = require("./builders/sync-builder");
function parse(text, option) {
option = option || {};
const builder = new sync_builder_1.SyncBuilder({
limit: option.limit || Infinity,
});
const iterator_builder_1 = require("./builders/iterator-builder");
function _parse(text, option) {
new parser_1.Parser({
builder,
builder: option.builder,
delimiter: option.delimiter,
}).parse(text);
return builder.getResult();
return option.builder.getResult();
}
function parse(text, option) {
return _parse(text, {
delimiter: option === null || option === void 0 ? void 0 : option.delimiter,
builder: new sync_builder_1.SyncBuilder({
limit: (option === null || option === void 0 ? void 0 : option.limit) || Infinity,
}),
});
}
exports.parse = parse;
function parseIterable() {
// TODO: implemented based on IteratorBUilder
function parseIterable(text, option) {
return _parse(text, {
delimiter: option === null || option === void 0 ? void 0 : option.delimiter,
builder: new iterator_builder_1.IteratorBuilder({
limit: (option === null || option === void 0 ? void 0 : option.limit) || Infinity,
}),
});
}
exports.parseIterable = parseIterable;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

@@ -48,13 +48,13 @@ "use strict";

const next = () => {
const resut = iterator.next();
if (resut.done) {
const result = iterator.next();
if (result.done) {
builder.onEnd();
return;
}
if (resut.value.type === 'cell') {
const cell = transform(resut.value.value);
if (result.value.type === 'cell') {
const cell = transform(result.value.value);
builder.onCell(cell);
next();
}
else if (resut.value.type === 'row') {
else if (result.value.type === 'row') {
builder.onRowEnd(next);

@@ -61,0 +61,0 @@ }

@@ -0,0 +0,0 @@ "use strict";

{
"name": "parse-csv-simple",
"version": "1.0.2",
"version": "1.0.3",
"description": "A parser for csv file.",

@@ -14,3 +14,3 @@ "main": "dist/index.js",

},
"keywords": [],
"keywords": ["csv", "parse csv"],
"author": "",

@@ -28,3 +28,6 @@ "license": "ISC",

"typescript": "^4.3.5"
},
"dependencies": {
"ts-node": "^10.1.0"
}
}

@@ -0,0 +0,0 @@ # parse-csv-simple

import { expect } from "chai";
import { parse } from "..";
import { parse, parseIterable } from "..";

@@ -32,2 +32,17 @@

});
describe('parseIterable', function() {
const csvText = "name,age,gender\nweilei,28,male";
it('should return an iterable object', function() {
const result = [...parseIterable(csvText)];
expect(result).has.lengthOf(2);
const row1 = result[0];
expect(row1).is.an('array');
expect(row1).has.lengthOf(3);
expect(row1.join(',')).equal('name,age,gender');
});
});
import { Builder } from "../interfaces";
import { isArrayEmpty } from "../utils";

@@ -8,3 +9,8 @@ interface IteratorBuilderOption {

limit: number;
next: (() => void) | null = null;
tempRow: any[] = [];
isEnd = false;
fetchedRows: number = 0;
constructor(option: IteratorBuilderOption) {

@@ -15,16 +21,34 @@ this.limit = option?.limit || Infinity;

onEnd() {
this.isEnd = true;
}
onCell() {
onCell(cell: any) {
this.tempRow.push(cell);
}
onRowEnd() {
onRowEnd(next: () => void) {
this.fetchedRows++;
if (this.fetchedRows >= this.limit) {
this.isEnd = true;
}
this.next = next;
}
getResult() {
return [];
const self = this;
return {
*[Symbol.iterator]() {
while(!self.isEnd) {
yield self.tempRow.slice();
self.tempRow = [];
self.next && self.next();
}
if (!isArrayEmpty(self.tempRow)) {
yield self.tempRow.slice();
}
}
};
}
}

@@ -0,0 +0,0 @@ import { Builder } from '../interfaces';

@@ -0,0 +0,0 @@ export const REGEXP_META_CHARS = [

import { Parser } from "./parser";
import { SyncBuilder } from './builders/sync-builder';
import { Builder } from "./interfaces";
import { IteratorBuilder } from "./builders/iterator-builder";
export function parse(text: string, option?: {limit?: number, delimiter?: string}) {
option = option || {};
const builder = new SyncBuilder({
limit: option.limit || Infinity,
});
function _parse(text: string, option: {delimiter?: string, builder: Builder}) {
new Parser({
builder,
builder: option.builder,
delimiter: option.delimiter,
}).parse(text);
return builder.getResult();
return option.builder.getResult();
}
export function parseIterable() {
// TODO: implemented based on IteratorBUilder
export function parse(text: string, option?: {limit?: number, delimiter?: string}) {
return _parse(text, {
delimiter: option?.delimiter,
builder: new SyncBuilder({
limit: option?.limit || Infinity,
}),
});
}
export function parseIterable(text: string, option?: {limit?: number, delimiter?: string}) {
return _parse(text, {
delimiter: option?.delimiter,
builder: new IteratorBuilder({
limit: option?.limit || Infinity,
}),
});
}

@@ -0,0 +0,0 @@

@@ -54,4 +54,4 @@ import { ParserOption, Builder, Transformer, ParsedResult } from './interfaces';

const next = () => {
const resut = iterator.next();
if (resut.done) {
const result = iterator.next();
if (result.done) {
builder.onEnd();

@@ -61,7 +61,7 @@ return;

if (resut.value.type === 'cell') {
const cell = transform(resut.value.value);
if (result.value.type === 'cell') {
const cell = transform(result.value.value);
builder.onCell(cell);
next();
} else if (resut.value.type === 'row') {
} else if (result.value.type === 'row') {
builder.onRowEnd(next);

@@ -68,0 +68,0 @@ }

@@ -0,0 +0,0 @@ import { REGEXP_META_CHARS } from '../consts';

@@ -0,0 +0,0 @@ {

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