Skip to content

Commit

Permalink
feature(formatify) add
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Jan 31, 2018
0 parents commit 121d129
Show file tree
Hide file tree
Showing 12 changed files with 367 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"env": {
"node": true
},
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
},
"rules": {
"indent": ["error", 4]
},
"extends": [
"eslint:recommended",
"plugin:node/recommended"
],
"plugins": [
"node"
],
}
15 changes: 15 additions & 0 deletions .eslintrc.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"env": {
"node": true
},
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
},
"rules": {
"indent": ["error", 4]
},
"extends": [
"eslint:recommended"
]
}
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package-lock.json
node_modules
npm-debug.log
coverage
legacy
.nyc_output

7 changes: 7 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.*
dist
test
coverage

*.swp

18 changes: 18 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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

21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
70 changes: 70 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

46 changes: 46 additions & 0 deletions lib/formatify.js
Original file line number Diff line number Diff line change
@@ -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
});
}

49 changes: 49 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "@cloudcmd/formatify",
"version": "1.0.0",
"author": "coderaiser <[email protected]> (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"
}
}
36 changes: 36 additions & 0 deletions test/fixture-raw.js
Original file line number Diff line number Diff line change
@@ -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,
};

34 changes: 34 additions & 0 deletions test/fixture.js
Original file line number Diff line number Diff line change
@@ -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--'
};

45 changes: 45 additions & 0 deletions test/formatify.js
Original file line number Diff line number Diff line change
@@ -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();
});

0 comments on commit 121d129

Please sign in to comment.