diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..d44aba0 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,19 @@ +{ + "env": { + "node": true + }, + "parserOptions": { + "ecmaVersion": 6, + "sourceType": "module" + }, + "rules": { + "indent": ["error", 4] + }, + "extends": [ + "eslint:recommended", + "plugin:node/recommended" + ], + "plugins": [ + "node" + ], +} diff --git a/.eslintrc.test b/.eslintrc.test new file mode 100644 index 0000000..d207366 --- /dev/null +++ b/.eslintrc.test @@ -0,0 +1,15 @@ +{ + "env": { + "node": true + }, + "parserOptions": { + "ecmaVersion": 6, + "sourceType": "module" + }, + "rules": { + "indent": ["error", 4] + }, + "extends": [ + "eslint:recommended" + ] +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..38ed380 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +package-lock.json +node_modules +npm-debug.log +coverage +legacy +.nyc_output + diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..cf21c3a --- /dev/null +++ b/.npmignore @@ -0,0 +1,7 @@ +.* +dist +test +coverage + +*.swp + diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..eb4d83d --- /dev/null +++ b/.travis.yml @@ -0,0 +1,18 @@ +language: node_js +node_js: + - 6 + - 8 + - 9 + +script: + - npm run lint + - npm run coverage + - npm run report + +notifications: + email: + on_success: never + on_failure: change + +sudo: false + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..eaec9a1 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) coderaiser + +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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..c711bfd --- /dev/null +++ b/README.md @@ -0,0 +1,70 @@ +# Formatify [![License][LicenseIMGURL]][LicenseURL] [![NPM version][NPMIMGURL]][NPMURL] [![Dependency Status][DependencyStatusIMGURL]][DependencyStatusURL] [![Build Status][BuildStatusIMGURL]][BuildStatusURL] [![Coverage Status][CoverageIMGURL]][CoverageURL] + +[NPMIMGURL]: https://img.shields.io/npm/v/@cloudcmd/formatify.svg?style=flat +[BuildStatusIMGURL]: https://img.shields.io/travis/cloudcmd/formatify/master.svg?style=flat +[DependencyStatusIMGURL]: https://img.shields.io/gemnasium/cloudcmd/formatify.svg?style=flat +[LicenseIMGURL]: https://img.shields.io/badge/license-MIT-317BF9.svg?style=flat +[NPMURL]: https://npmjs.org/package/@cloudcmd/formatify "npm" +[BuildStatusURL]: https://travis-ci.org/cloudcmd/formatify "Build Status" +[DependencyStatusURL]: https://gemnasium.com/cloudcmd/formatify "Dependency Status" +[LicenseURL]: https://tldrlegal.com/license/mit-license "MIT License" + +Format directory content received by [readify](https://github.com/coderaiser/readify). + +## Install + +``` +npm i @cloudcmd/formatify --save +``` + +## API + +### formatify(files) +- **files** - `files list` + +## Examples + +```js +const files = [{ + name: 'sortify.js', + size: 3538, + date, + owner: 0, + mode: 33204, +}, { + name: 'readify.js', + size: 1629, + date: 2016-11-21T13:37:55.275Z, + owner: 0, + mode: 33204, +}]; + +formatify(files) +// returns +[{ + name: 'formatify.js', + size: '3.46kb', + date: '12.01.2017', + owner: 0, + mode: 'rw- rw- r--', +}, { + name: 'readify.js', + size: '1.59kb', + date: '12.01.2017', + owner: 0, + mode: 'rw- rw- r--', +}]; +``` + +## Related + +- [Sortify](https://github.com/cloudcmd/sortify "Sortify") - sort directory content by name, size, date +- [Readify](https://github.com/coderaiser/readify "Readify") - read directory content with file attributes: size, date, owner, mode + +## License + +MIT + +[CoverageURL]: https://coveralls.io/github/cloudcmd/formatify?branch=master +[CoverageIMGURL]: https://coveralls.io/repos/cloudcmd/formatify/badge.svg?branch=master&service=github + diff --git a/lib/formatify.js b/lib/formatify.js new file mode 100644 index 0000000..ddd75e7 --- /dev/null +++ b/lib/formatify.js @@ -0,0 +1,46 @@ +'use strict'; + +const format = require('format-io'); +const shortdate = require('shortdate'); + +module.exports = (files) => { + check(files); + + return files + .map(replaceDate) + .map(replaceMode) + .map(replaceSize); +}; + +function check(files) { + if (!Array.isArray(files)) + throw Error('files should be an array!'); +} + +function replaceDate(stat) { + const date = !stat.date ? '' : shortdate(stat.date, { + order: 'little' + }); + + return Object.assign(stat, { + date + }); +} + +function replaceMode(stat) { + const octal = Number(stat.mode).toString(8); + const mode = format.permissions.symbolic(octal); + + return Object.assign(stat, { + mode + }); +} + +function replaceSize(stat) { + const size = format.size(stat.size); + + return Object.assign(stat, { + size + }); +} + diff --git a/package.json b/package.json new file mode 100644 index 0000000..2db45d7 --- /dev/null +++ b/package.json @@ -0,0 +1,49 @@ +{ + "name": "@cloudcmd/formatify", + "version": "1.0.0", + "author": "coderaiser (https://github.com/coderaiser)", + "description": "format directory content", + "homepage": "http://github.com/cloudcmd/formatify", + "repository": { + "type": "git", + "url": "git://github.com/cloudcmd/formatify.git" + }, + "scripts": { + "lint": "redrun lint:*", + "lint:eslint:lib": "eslint lib", + "lint:eslint:test": "eslint -c .eslintrc.test test --no-eslintrc", + "report": "nyc report --reporter=text-lcov | coveralls", + "coverage": "nyc npm test", + "test": "tape test/*.js", + "watch:coverage": "npm run watcher -- npm run coverage", + "watch:test": "npm run watcher -- npm test", + "watcher": "nodemon -w test -w lib --exec" + }, + "dependencies": { + "format-io": "^0.9.6", + "shortdate": "^1.2.0" + }, + "keywords": [ + "sort", + "file", + "directory", + "size", + "name", + "date" + ], + "license": "MIT", + "main": "lib/formatify.js", + "engines": { + "node": ">=4" + }, + "devDependencies": { + "coveralls": "^3.0.0", + "eslint": "^4.0.0", + "eslint-plugin-node": "^5.1.0", + "mkdirp": "^0.5.1", + "nodemon": "^1.11.0", + "nyc": "^11.0.2", + "redrun": "^5.0.1", + "tape": "^4.2.1" + } +} diff --git a/test/fixture-raw.js b/test/fixture-raw.js new file mode 100644 index 0000000..d06177b --- /dev/null +++ b/test/fixture-raw.js @@ -0,0 +1,36 @@ +'use strict'; + +const date = new Date('2017-01-12T09:01:35.288Z'); + +module.exports.sortifyRaw = { + name: 'sortify.js', + size: 3538, + date, + owner: 0, + mode: 33204, +}; + +module.exports.testRaw = { + name: 'test', + size: 'dir', + date, + owner: 0, + mode: 33204, +}; + +module.exports.readifyRaw = { + name: 'readify.js', + size: 1629, + date, + owner: 0, + mode: 33204, +}; + +module.exports.libRaw = { + name: 'lib', + size: 'dir', + date: 0, + owner: 0, + mode: 33204, +}; + diff --git a/test/fixture.js b/test/fixture.js new file mode 100644 index 0000000..61a2deb --- /dev/null +++ b/test/fixture.js @@ -0,0 +1,34 @@ +'use strict'; + +module.exports.sortifyFile = { + name: 'sortify.js', + size: '3.46kb', + date: '12.01.2017', + owner: 0, + mode: 'rw- rw- r--', +}; + +module.exports.testDir = { + name: 'test', + size: 'dir', + date: '12.01.2017', + owner: 0, + mode: 'rw- rw- r--', +}; + +module.exports.readifyFile = { + name: 'readify.js', + size: '1.59kb', + date: '12.01.2017', + owner: 0, + mode: 'rw- rw- r--', +}; + +module.exports.libDir = { + name: 'lib', + size: 'dir', + date: '', + owner: 0, + mode: 'rw- rw- r--' +}; + diff --git a/test/formatify.js b/test/formatify.js new file mode 100644 index 0000000..df0730c --- /dev/null +++ b/test/formatify.js @@ -0,0 +1,45 @@ +'use strict'; + +const formatify = require('..'); +const test = require('tape'); + +const { + testDir, + libDir, + readifyFile, + sortifyFile, +} = require('./fixture'); + +const { + testRaw, + libRaw, + readifyRaw, + sortifyRaw +} = require('./fixture-raw'); + +test('arguments: exception when no files', t => { + t.throws(formatify, /files should be an array!/, 'should throw when no files'); + t.end(); +}); + +test('result', (t) => { + const files = [ + testRaw, + libRaw, + readifyRaw, + sortifyRaw, + ]; + + const expected = [ + testDir, + libDir, + readifyFile, + sortifyFile, + ]; + + const result = formatify(files); + + t.deepEqual(result, expected, 'should equal'); + t.end(); +}); +