-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
154 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
language: node_js | ||
node_js: | ||
- "4.4" | ||
- "6.1" | ||
after_script: | ||
- './node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls' | ||
cache: | ||
directories: | ||
- node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,37 @@ | ||
# filename-converter | ||
Convert a invalid filename to a valid one using self-defined mapping table. The key point is the mapping is reversible. | ||
|
||
[![Build Status](https://travis-ci.org/Maples7/filename-converter.svg?branch=master)](https://travis-ci.org/Maples7/filename-converter) | ||
[![Coverage Status](https://coveralls.io/repos/github/Maples7/filename-converter/badge.svg?branch=master)](https://coveralls.io/github/Maples7/filename-converter?branch=master) | ||
[![npm version](https://badge.fury.io/js/filename-converter.svg)](https://badge.fury.io/js/filename-converter) | ||
|
||
[![NPM](https://nodei.co/npm/filename-converter.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/filename-converter/) | ||
[![NPM](https://nodei.co/npm-dl/filename-converter.png?months=6&height=3)](https://nodei.co/npm/filename-converter/) | ||
|
||
Convert an invalid filename to a valid one with a self-defined mapping table. The key point is the mapping is reversible. | ||
|
||
## Invalid Characters | ||
|
||
`/<>:"\|?*` is the current set of invalid characters, see [mapping_table.js](mapping_table.js) to get the mapping table. | ||
|
||
## Usage | ||
|
||
### Installation | ||
|
||
```sh | ||
npm i filename-converter | ||
``` | ||
|
||
### Example | ||
|
||
```js | ||
const filenameConverter = require('filename-converter'); | ||
|
||
const filename = 'abc:de<.json'; | ||
const ret = filenameConverter.serialize(filename); | ||
console.log(ret); // => abc@004de@002.json | ||
console.log(filenameConverter.deserialize(ret)); // => abc:de<.json | ||
``` | ||
|
||
## LICENSE | ||
|
||
[MIT](LICENSE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict'; | ||
|
||
const mappingTable = require('./mapping_table'); | ||
|
||
module.exports = { | ||
serialize: function serialize(str) { | ||
const ret = []; | ||
const table = mappingTable.forward; | ||
for (let i = 0; i < str.length; i++) { | ||
ret.push(table[str[i]] ? table[str[i]] : str[i]); | ||
} | ||
return ret.join(''); | ||
}, | ||
deserialize: function deserialize(str) { | ||
const ret = []; | ||
const table = mappingTable.reverse; | ||
const keys = Object.keys(table); | ||
for (let i = 0; i < str.length; i++) { | ||
if (str[i] === '@') { | ||
const tmp = str.slice(i, i + 4); | ||
if (keys.indexOf(tmp) > -1) { | ||
ret.push(table[tmp]); | ||
i += 3; | ||
} else { | ||
ret.push(str[i]); | ||
} | ||
} else { | ||
ret.push(str[i]); | ||
} | ||
} | ||
return ret.join(''); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use strict'; | ||
|
||
const forward = { | ||
'/': '@001', | ||
'<': '@002', | ||
'>': '@003', | ||
':': '@004', | ||
'"': '@005', | ||
'\\': '@006', | ||
'|': '@007', | ||
'?': '@008', | ||
'*': '@009' | ||
}; | ||
|
||
const reverse = {}; | ||
Object.keys(forward).forEach(key => { | ||
reverse[forward[key]] = key; | ||
}); | ||
|
||
module.exports = { | ||
forward: forward, | ||
reverse: reverse | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "filename-converter", | ||
"version": "1.0.0", | ||
"description": "Convert a invalid filename to a valid one with a self-defined mapping table. The key point is the mapping is reversible.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "nyc ava", | ||
"coverage": "./node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls" | ||
}, | ||
"nyc": { | ||
"exclude": ["test*.js"] | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/Maples7/filename-converter.git" | ||
}, | ||
"author": "Maples7 <maples7.com>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/Maples7/filename-converter/issues" | ||
}, | ||
"homepage": "https://github.com/Maples7/filename-converter#readme", | ||
"devDependencies": { | ||
"ava": "^0.21.0", | ||
"coveralls": "^2.13.1", | ||
"nyc": "^11.1.0" | ||
}, | ||
"engines": { | ||
"node": ">=4.4.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict'; | ||
|
||
const test = require('ava'); | ||
const filenameConverter = require('./index'); | ||
|
||
test('A valid filename', t => { | ||
t.plan(2); | ||
const testData = 'IAmANormalFileName.json' | ||
const ret1 = filenameConverter.serialize(testData); | ||
const ret2 = filenameConverter.deserialize(ret1); | ||
t.is(ret1, testData); | ||
t.is(ret2, testData); | ||
}); | ||
|
||
test('Few invalid characters', t => { | ||
t.plan(2); | ||
const testData = '|//*as\\d"*@010'; | ||
const ret1 = filenameConverter.serialize(testData); | ||
const ret2 = filenameConverter.deserialize(ret1); | ||
t.is(ret1, '@007@001@001@009as@006d@005@009@010'); | ||
t.is(ret2, testData); | ||
}); |