Skip to content

Commit

Permalink
import
Browse files Browse the repository at this point in the history
  • Loading branch information
touv committed Jan 11, 2013
1 parent 80e1301 commit 24b2dac
Show file tree
Hide file tree
Showing 217 changed files with 42,670 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
*.tgz
*.swp
*.swo
README.html
1 change: 1 addition & 0 deletions .npmignore
@@ -0,0 +1 @@
.git*
3 changes: 3 additions & 0 deletions .travis.yml
@@ -0,0 +1,3 @@
language: node_js
node_js:
- 0.6
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
Copyright 2011 Nicolas Thouvenin <nthouvenin@gmail.com>

This project is free software released under the MIT/X11 license:

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
41 changes: 41 additions & 0 deletions README.md
@@ -0,0 +1,41 @@

# XMLWriter for NodeJS

[![Build Status](https://secure.travis-ci.org/touv/node-csv-tools.png?branch=master)](http://travis-ci.org/touv/node-csv-tools)

It's a collection of javascript tools for any kind of CSV. It tries to be tolerant.
It can be used in a browser or with nodejs.

## Contributors

* [Nicolas Thouvenin](https://github.com/touv)

# Installation

With [npm](http://npmjs.org) do:

$ # Coming soon


# Examples


# Tests

Use [mocha](https://github.com/visionmedia/mocha) to run the tests.

$ npm install mocha
$ mocha test

# API Documentation


# Also

* https://npmjs.org/browse/keyword/csv
* http://www.uselesscode.org/javascript/csv/
* https://github.com/archan937/csonv.js

# License

[MIT/X11](./LICENSE)
1 change: 1 addition & 0 deletions index.js
@@ -0,0 +1 @@
module.exports = require('./lib/csv.js');
165 changes: 165 additions & 0 deletions lib/csv.js
@@ -0,0 +1,165 @@
'use strict';

function reducer(item, memo, sep) {
var foo = ''
, c = new RegExp(sep, 'g')
, q = new RegExp('"', 'g')
, n = new RegExp('\n', 'g');

if (!item) {
item = '';
}
if (typeof item != 'string') {
var s = item.toString();
if (s == '[object Object]') {
item = JSON.stringify(item);
if (item == '{}') {
item = '';
}
}
else {
item = s;
}
}
if (memo) {
foo = memo + sep;
}
if (item.search(c) >= 0 || item.search(q) >= 0 || item.search(n) >= 0) {
foo += '"' + item.replace(q, '""') + '"';
}
else {
foo += ''+item;
}
return foo;
}


exports.separator = function(data)
{
var separators = [{c: 0, v: ','}, {c: 0, v: ';'}, {c: 0, v: '|'}, {c: 0, v: '\t'}, {c:0, v:null}];
separators.forEach(function (item, indice) {
item.c += data.split(item.v).length;
}
);
var max = 0;
var separator = separators.reduce(function (prev, cur) {
if (cur.c >= max) {
max = cur.c;
return cur.v;
}
else {
return prev;
}
}, null
);
return separator;
}


exports.stringify = function(input, sep) {
var ret = '';
sep = sep || ",";
if (Array.isArray(input)) {
input.forEach(function(item) {
ret = reducer(item, ret, sep);
});
}
else if (typeof input == 'object') {
for (var key in input) {
if (input.hasOwnProperty(key)) {
ret = reducer(input[key], ret, sep);
}
}
}
else {
ret = reducer(input, ret, sep);
}
return ret + "\r\n";
}

exports.parse = function(input, sep) {

var output = [],
remainder = exports.forEach(sep, function(row, index) {

})
}


exports.forEach = function(input, sep, callback) {

}

exports.read = function(input, sep, callback) {
sep = sep || ",";
var fields = [], reminderIndex = 0, token = '', endingIndex = 0, endingLine = 0, startingIndex = 0,
sepx = new RegExp('[^\\s'+sep+']+.');

// console.log('input:', input);
// console.log('input.length:', input.length);
// console.log('sep:', sep);
while (startingIndex <= input.length ) {
endingIndex = input.indexOf(sep, startingIndex);
endingLine = input.indexOf('\n', startingIndex);
// console.log('startingIndex:', startingIndex);
// console.log('endingIndex:', endingIndex);
// console.log('endingLine:', endingLine);

if (endingIndex < 0) {
endingIndex = input.length;
}
token = input.slice(startingIndex, endingIndex).trim();
// console.log('token:', token);
if (token.charAt(0) == '"' && token.charAt(token.length - 1) == '"') {
// D : "token"
fields.push(token.slice(1, -1));
startingIndex = endingIndex + 1;
}
else if (token.charAt(0) == '"') {
// A : "token
//
var s = input.slice(startingIndex + 1);
if (s.indexOf('"') < 0) {
reminderIndex = startingIndex;
callback(fields);
return reminderIndex;
}
else {
var i = s.search(/[^\"]\"(?!\")/) + 1;
fields.push(input.slice(startingIndex + 1, startingIndex + i + 1));
var j = input.slice(startingIndex + i + 2).search(sepx);
startingIndex += i + 2 + (j >= 0 ? j : 0);
}
}
else if (token.charAt(token.length - 1) == '"') {
// C : token"
fields.push(input.slice(startingIndex, endingIndex));
startingIndex = endingIndex + 1;
}
else {
// B : token
// console.log(endingLine, '<', endingIndex);
if (endingLine >= 0 && endingLine < endingIndex) {
fields.push(input.slice(startingIndex, endingLine));
reminderIndex = endingLine + 1;
callback(fields);
return reminderIndex;
}
fields.push(input.slice(startingIndex, endingIndex));
startingIndex = endingIndex + 1;
}
}
reminderIndex = null;
callback(fields);
return reminderIndex;
}

exports.fetch = function(input, sep) {

var output;
exports.read(input, sep, function(fields) {
output = fields;
});
return output;

}
1 change: 1 addition & 0 deletions node_modules/.bin/_mocha

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/mocha

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions node_modules/mocha/.npmignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions node_modules/mocha/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 24b2dac

Please sign in to comment.