From d522ac688392df1f6cd39025de5d38f417c9701a Mon Sep 17 00:00:00 2001 From: David Kebler Date: Sat, 14 Dec 2024 18:45:07 -0800 Subject: [PATCH] refactor and improve both functions uses promise.all now --- .gitignore | 1 + api.md | 37 +++++++++++++++++++++++ package.json | 6 ++-- readme.md | 5 ++++ src/read-lines.js | 65 ++++++++++++++++++++--------------------- test/combined.list | 7 ----- test/read-lines.test.js | 24 +++++++++++++-- 7 files changed, 99 insertions(+), 46 deletions(-) create mode 100644 api.md delete mode 100644 test/combined.list diff --git a/.gitignore b/.gitignore index e61051f..f1b4f87 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /node_modules/ /coverage/ +test/combined2.list diff --git a/api.md b/api.md new file mode 100644 index 0000000..0abf929 --- /dev/null +++ b/api.md @@ -0,0 +1,37 @@ +## Functions + +
+
readLines(files, dir)Promise.<array>
+

read lines from one or more files and return an array of those all those lines

+
+
writeLines(filePath, list)Promise.<null>
+

Writes files with a line for each element in a array or item in comma delimited list

+
+
+ + + +## readLines(files, dir) ⇒ Promise.<array> +read lines from one or more files and return an array of those all those lines + +**Kind**: global function +**Returns**: Promise.<array> - - resolves to an single array of all lines from all files (no guarenteed order) + +| Param | Type | Description | +| --- | --- | --- | +| files | array \| string | array or comma delimited string of file paths. Can be absolute or relative (see dir) | +| dir | string | the directory to join to any relative paths | + + + +## writeLines(filePath, list) ⇒ Promise.<null> +Writes files with a line for each element in a array or item in comma delimited list + +**Kind**: global function +**Returns**: Promise.<null> - can catch errors but otherwise nothing resolves + +| Param | Type | Description | +| --- | --- | --- | +| filePath | string | filepath to which file is written | +| list | array \| string | each element becomes line in file | each comma delimited item becomes a line | + diff --git a/package.json b/package.json index 628a4cb..9d1f710 100755 --- a/package.json +++ b/package.json @@ -28,9 +28,7 @@ "url": "https://github.com/uCOMmandIt/uci-utils/issues" }, "homepage": "https://github.com/uCOMmandIt/uci-utils#readme", - "dependencies": { - "p-settle": "^5.1.1" - }, + "dependencies": {}, "devDependencies": { "chai": "^5.1.2", "chai-arrays": "^2.2.0", @@ -38,4 +36,4 @@ "mocha": "^11.0.1", "nodemon": "^3.1.9" } -} +} \ No newline at end of file diff --git a/readme.md b/readme.md index 33679be..30479a4 100644 --- a/readme.md +++ b/readme.md @@ -1,2 +1,7 @@ ### Read Lines to Array #### a uCOMmandIt Utiltiy Function + + +exports two functions, readlines, writelines + +[API](./api.md) \ No newline at end of file diff --git a/src/read-lines.js b/src/read-lines.js index 51c3127..10376be 100644 --- a/src/read-lines.js +++ b/src/read-lines.js @@ -1,50 +1,49 @@ -import { promisify } from 'util' -import path from 'path' -import { readFile, writeFile } from 'fs' -const read = promisify(readFile) -const write = promisify(writeFile) -import settle from 'p-settle' -// import logger from '@uci-utils/logger' -// let log = logger({ package:'@uci-utils/read-lines', file:'src/read-lines.js'}) +import path from 'node:path' +import { readFile as read, writeFile as write, stat } from 'node:fs/promises' -// read lines from one or more files + +/** + * read lines from one or more files and return an array of those all those lines + * @param {array|string} files - array or comma delimited string of file paths. Can be absolute or relative (see dir) + * @param {string} dir - the directory to join to any relative paths + * @returns {Promise} - resolves to an single array of all lines from all files (no guarenteed order) + */ function readLines (files=[],dir) { - // log.debug({files:files,dir:dir,msg:'additional files'}) let list = [] - if (!Array.isArray(files)) files=[files] - // each set in an the array is new line delimited set of ignore patterns - // settle returns array of error,value pairs - return settle(files.map(file => { - // console.log('directory',path.dirname(file)) - if (path.dirname(file)==='.') file = dir+'/'+file - // log.debug({function:'readLines',file:file,msg:'reading a file of lines into array'}) - return read(file) + // if not an array of file names it must be a string + if (!Array.isArray(files)) files=files.split(',') + return Promise.all(files.map(async file => { + if (! path.isAbsolute(file) ) { + if ( typeof dir === 'string' ) { + file = path.join(dir,file) + }} + return await read(file) + .then(lines => { + list.push(...lines.toString().match(/.+/g)) + return Promise.resolve(lines) + }) + .catch(e => { + return Promise.reject(e) + }); })) .then((sets) => { - sets.forEach( set => { - if (set.isFulfilled) list.push(...set.value.toString().match(/.+/g)) - else - console.log("unable to read file") - // log.warn({function:'readLines', error:set.reason, msg:' was unable to read file'}) - }) return Promise.resolve(list) }) .catch((err) => { - // only returned when something horrible is wrong return Promise.reject(err) }) } -// an ignore list should not be huge so can serailize at once -function writeLines (filePath,list) { +/** + * Writes files with a line for each element in a array or item in comma delimited list + * @param {string} filePath - filepath to which file is written + * @param {array|string} list - each element becomes line in file | each comma delimited item becomes a line + * @returns {Promise} can catch errors but otherwise nothing resolves + */ +function writeLines (filePath,list) { + if (typeof list === "string") list = list.split(',') return write(filePath,list.join('\n')) - .then(() => { - // log.info({function:'writeLines', file:filePath, msg:'wrote array to file of lines'}) - }) - .catch( err => { - // log.fatal({function:'writeLines', error:err, msg:'unable to write array to file of lines'}) - }) } export default readLines diff --git a/test/combined.list b/test/combined.list deleted file mode 100644 index 6115502..0000000 --- a/test/combined.list +++ /dev/null @@ -1,7 +0,0 @@ -/node_modules/ -/coverage/ -tests/ -test/ -*.test.js -testing/ -example/ \ No newline at end of file diff --git a/test/read-lines.test.js b/test/read-lines.test.js index a70bdc8..4413084 100644 --- a/test/read-lines.test.js +++ b/test/read-lines.test.js @@ -1,5 +1,3 @@ -// let ignoreFiles = ['.npmignore','.gitignore'] - import { readLines, writeLines } from '../src/read-lines.js' import { it } from 'mocha' import { dirname } from 'dirname-filename-esm'; @@ -41,6 +39,20 @@ function readList() { let result = await readLines(['./test/.gitignore',__dirname+'/.npmignore']) expect(result, 'list build failed').to.be.containingAllOf(shouldbe) }) + + it('==> can read files passed as a comma delimited string', async function () { + const shouldbe = [ 'tests/', + 'test/', + '*.test.js', + 'testing/', + 'example/', + '/node_modules/', + '/coverage/' ] + let result = await readLines('test/.gitignore,test/.npmignore') + expect(result, 'list build failed').to.be.containingAllOf(shouldbe) + }) + + } @@ -52,4 +64,12 @@ function writeList() { const result = await readLines(['combined.list'],__dirname) expect(result, 'list build failed').to.be.containingAllOf(shouldbe) }) + + it('==> can write an comma delimited string of items as lines in a file', async function () { + const shouldbe = await readLines(['.gitignore','.npmignore'],__dirname) + await writeLines(__dirname+'/combined2.list',shouldbe.join(',')) + const result = await readLines(['combined2.list'],__dirname) + expect(result, 'list build failed').to.be.containingAllOf(shouldbe) + }) + }