Skip to content
This repository was archived by the owner on Jun 28, 2021. It is now read-only.

Commit 3b85a24

Browse files
wintonwdavidw
authored andcommitted
Handle duplicate column names if columns: true
1 parent fb07f2b commit 3b85a24

File tree

3 files changed

+63
-15
lines changed

3 files changed

+63
-15
lines changed

lib/es5/index.js

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,31 @@
22

33
function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); }
44

5-
function isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
6-
7-
function _construct(Parent, args, Class) { if (isNativeReflectConstruct()) { _construct = Reflect.construct; } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
5+
function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct; } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
86

97
function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; }
108

119
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
1210

13-
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
11+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
1412

15-
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
13+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
1614

17-
function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
15+
function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
1816

1917
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
2018

21-
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
19+
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
20+
21+
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
22+
23+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(n); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
2224

23-
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
25+
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
2426

25-
function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
27+
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
2628

27-
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
29+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
2830

2931
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
3032

@@ -38,10 +40,14 @@ function _defineProperties(target, props) { for (var i = 0; i < props.length; i+
3840

3941
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
4042

43+
function _createSuper(Derived) { return function () { var Super = _getPrototypeOf(Derived), result; if (_isNativeReflectConstruct()) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
44+
4145
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
4246

4347
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
4448

49+
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
50+
4551
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
4652

4753
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
@@ -69,16 +75,18 @@ var bom_utf8 = Buffer.from([239, 187, 191]);
6975
var Parser = /*#__PURE__*/function (_Transform) {
7076
_inherits(Parser, _Transform);
7177

78+
var _super = _createSuper(Parser);
79+
7280
function Parser() {
7381
var _this;
7482

7583
var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
7684

7785
_classCallCheck(this, Parser);
7886

79-
_this = _possibleConstructorReturn(this, _getPrototypeOf(Parser).call(this, _objectSpread({}, {
87+
_this = _super.call(this, _objectSpread({}, {
8088
readableObjectMode: true
81-
}, {}, opts)));
89+
}, {}, opts));
8290
var options = {}; // Merge with user options
8391

8492
for (var opt in opts) {
@@ -875,7 +883,16 @@ var Parser = /*#__PURE__*/function (_Transform) {
875883

876884
for (var i = 0, l = record.length; i < l; i++) {
877885
if (columns[i] === undefined || columns[i].disabled) continue;
878-
obj[columns[i].name] = record[i];
886+
887+
if (obj[columns[i].name]) {
888+
if (Array.isArray(obj[columns[i].name])) {
889+
obj[columns[i].name] = obj[columns[i].name].concat(record[i]);
890+
} else {
891+
obj[columns[i].name] = [obj[columns[i].name], record[i]];
892+
}
893+
} else {
894+
obj[columns[i].name] = record[i];
895+
}
879896
}
880897

881898
var objname = this.options.objname;
@@ -1291,13 +1308,15 @@ var parse = function parse() {
12911308
var CsvError = /*#__PURE__*/function (_Error) {
12921309
_inherits(CsvError, _Error);
12931310

1311+
var _super2 = _createSuper(CsvError);
1312+
12941313
function CsvError(code, message) {
12951314
var _this2;
12961315

12971316
_classCallCheck(this, CsvError);
12981317

12991318
if (Array.isArray(message)) message = message.join(' ');
1300-
_this2 = _possibleConstructorReturn(this, _getPrototypeOf(CsvError).call(this, message));
1319+
_this2 = _super2.call(this, message);
13011320

13021321
if (Error.captureStackTrace !== undefined) {
13031322
Error.captureStackTrace(_assertThisInitialized(_this2), CsvError);

lib/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,15 @@ class Parser extends Transform {
739739
// Transform record array to an object
740740
for(let i = 0, l = record.length; i < l; i++){
741741
if(columns[i] === undefined || columns[i].disabled) continue
742-
obj[columns[i].name] = record[i]
742+
if (obj[columns[i].name]) {
743+
if (Array.isArray(obj[columns[i].name])) {
744+
obj[columns[i].name] = obj[columns[i].name].concat(record[i])
745+
} else {
746+
obj[columns[i].name] = [obj[columns[i].name], record[i]]
747+
}
748+
} else {
749+
obj[columns[i].name] = record[i]
750+
}
743751
}
744752
const {objname} = this.options
745753
if(objname === undefined){

test/option.columns.coffee

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,27 @@ describe 'Option `columns`', ->
7575
'FIELD_6': '2050-11-27'
7676
] unless err
7777
next err
78+
79+
it 'handle duplicate column names if true', (next) ->
80+
parse """
81+
FIELD_1,FIELD_2,FIELD_3,FIELD_4,FIELD_5,FIELD_5
82+
20322051544,1979,8.8017226E7,ABC,45,2000-01-01
83+
28392898392,1974,8.8392926E7,DEF,23,2050-11-27
84+
""", columns: true, (err, data) ->
85+
data.should.eql [
86+
'FIELD_1': '20322051544'
87+
'FIELD_2': '1979'
88+
'FIELD_3': '8.8017226E7'
89+
'FIELD_4': 'ABC'
90+
'FIELD_5': ['45', '2000-01-01']
91+
,
92+
'FIELD_1': '28392898392'
93+
'FIELD_2': '1974'
94+
'FIELD_3': '8.8392926E7'
95+
'FIELD_4': 'DEF'
96+
'FIELD_5': ['23', '2050-11-27']
97+
] unless err
98+
next err
7899

79100
it 'disabled if false', (next) ->
80101
parse """

0 commit comments

Comments
 (0)