Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can you review this feature? #129

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ fs.writeJSON = jsonFile.writeFile
fs.writeJsonSync = jsonFile.writeFileSync
fs.writeJSONSync = jsonFile.writeFileSync

var row2arr = require('./row2arr')
fs.readRow2Arr = row2arr.readRow2Arr
fs.readRow2ArrSync = row2arr.readRow2ArrSync

var empty = require('./empty-dir')
Object.keys(empty).forEach(function (method) {
fs[method] = empty[method]
Expand Down
47 changes: 47 additions & 0 deletions lib/row2arr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
var fs = require('graceful-fs')
var os = require('os')

function withoutCommentLine (arr) {
var arrLength = arr.length
var filterArr = []

for (var i = 0; i < arrLength; i++) {
var checkText = arr[i]

if (/^#.*/.test(checkText) === false) {
filterArr.push(checkText)
}
}

return filterArr
}

function readRow2Arr (file, callback) {
fs.readFile(file, {
encoding: 'utf-8'
}, function (err, data) {
if (err) {
callback(err)
return false
}

var rowarr = data.split(os.EOL)

callback(err, withoutCommentLine(rowarr))
})
}

function readRow2ArrSync (file) {
var data = fs.readFileSync(file, {
encoding: 'utf-8'
})

var rowarr = data.split(os.EOL)

return withoutCommentLine(rowarr)
}

module.exports = {
readRow2Arr: readRow2Arr,
readRow2ArrSync: readRow2ArrSync
}
65 changes: 65 additions & 0 deletions test/row2arr/read.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
var assert = require('assert')
var fs = require('fs')
var path = require('path')
var os = require('os')
var fse = require(process.cwd())

/* global afterEach, beforeEach, describe, it */

describe('read', function () {
var TEST_DIR

beforeEach(function (done) {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'read-json')
fse.emptyDir(TEST_DIR, done)
})

afterEach(function (done) {
fse.remove(TEST_DIR, done)
})

describe('+ convert to array', function () {
it('should read a file and convert row to arr', function (done) {
var text = 'aaaa\nbbbb\ncccc\ndddd'

var file = path.join(TEST_DIR, 'file.txt')
fs.writeFileSync(file, text)

fse.readRow2Arr(file, function (err, arr) {
assert.ifError(err)
assert.strictEqual(arr[0], 'aaaa')
assert.strictEqual(arr[1], 'bbbb')
assert.strictEqual(arr[2], 'cccc')

done()
})
})

it('should read a file and convert row to arr by sync', function () {
var text = 'aaaa\nbbbb\ncccc\ndddd'

var file = path.join(TEST_DIR, 'file.txt')
fs.writeFileSync(file, text)

var rowarr = fse.readRow2ArrSync(file)

assert.strictEqual(rowarr[0], 'aaaa')
assert.strictEqual(rowarr[1], 'bbbb')
assert.strictEqual(rowarr[2], 'cccc')
})

it('should be ignore comment line', function () {
var text = 'aaaa\nbbbb\ncccc\n# ignore line\ndddd'

var file = path.join(TEST_DIR, 'file.txt')
fs.writeFileSync(file, text)

var rowarr = fse.readRow2ArrSync(file)

assert.strictEqual(rowarr[0], 'aaaa')
assert.strictEqual(rowarr[1], 'bbbb')
assert.strictEqual(rowarr[2], 'cccc')
assert.strictEqual(rowarr[3], 'dddd')
})
})
})