Skip to content

Commit

Permalink
update test deps, some refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jehy committed Apr 12, 2019
1 parent 3747ff3 commit 4a73d8f
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 134 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
node_modules
coverage
coverage
package-lock.json
6 changes: 6 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.idea
node_modules
coverage
package-lock.json
test
.eslintrc.json
10 changes: 6 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ node_js:
- "0.10"
- "0.12"
- "4"
- "5"
- "6"
- "node"
- "iojs"
- "7"
- "8"
- "9"
- "10"
- "11"
after_success:
- npm run coverage && npm run coveralls
- npm run coverage && npm run coveralls
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 Jehy

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.
29 changes: 16 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
#Mak sensitive

# Mask sensitive
[![npm](https://img.shields.io/npm/v/mask-sensitive.svg)](https://npm.im/mask-sensitive)
[![license](https://img.shields.io/npm/l/mask-sensitive.svg)](https://npm.im/mask-sensitive)
[![Build Status](https://travis-ci.org/jehy/mask-sensitive.svg?branch=master)](https://travis-ci.org/jehy/mask-sensitive)
[![Coverage Status](https://coveralls.io/repos/github/jehy/mask-sensitive/badge.svg?branch=master)](https://coveralls.io/github/jehy/mask-sensitive?branch=master)
[![dependencies Status](https://david-dm.org/jehy/mask-sensitive/status.svg)](https://david-dm.org/jehy/mask-sensitive)
[![devDependencies Status](https://david-dm.org/jehy/mask-sensitive/dev-status.svg)](https://david-dm.org/jehy/mask-sensitive?type=dev)

Masks sensitive data with several different methods.

##Installation
## Installation

```bash
npm install mask-sensitive
```

##Usage
## Usage

```js
var
mask = require('mask-sensitive'),
password = 'very sensitive data',
masked = mask(password, {mode: 'secure'});
var mask = require('mask-sensitive');
var password = 'very sensitive data';
var masked = mask(password, {mode: 'secure'});
```

## Modes

* `secure` - return "***";
* `length` - return equal to length number of "*";
* `secure` - return `***`;
* `length` - return equal to length number of `*`;
* `half` - mask first and last 1/4;
* `every` - mask every third char;
* `middle` - replace middle with "***";
* `middle` - replace middle with `***`;

By default, `secure` method is used.

##Important
## Important
* All data less then 4 chars will be masked with `secure` mode;
* null is masked as ***.
* null is masked as ***.
23 changes: 11 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mask-sensitive",
"version": "0.0.6",
"version": "0.0.7",
"description": "mask sensitive data",
"keywords": [
"data",
Expand All @@ -21,17 +21,16 @@
"author": "Jehy",
"license": "MIT",
"devDependencies": {
"coveralls": "^2.12.0",
"istanbul": "^0.4.5",
"mocha": "^3.2.0",
"babel-cli": "^6.23.0",
"babel-preset-es2015": "^6.22.0",
"eslint": "^3.16.0",
"eslint-config-airbnb-base": "^11.1.0",
"chai": "^4.2.0",
"coveralls": "^3.0.3",
"eslint": "^5.16.0",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-jsx-a11y": "^4.0.0",
"eslint-plugin-promise": "^3.4.2",
"eslint-plugin-react": "^6.10.0",
"eslint-plugin-standard": "^2.0.1"
"eslint-plugin-jsx-a11y": "^6.2.1",
"eslint-plugin-promise": "^4.1.1",
"eslint-plugin-react": "^7.12.4",
"eslint-plugin-standard": "^4.0.0",
"istanbul": "^0.4.5",
"mocha": "^6.1.2"
}
}
32 changes: 16 additions & 16 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'use strict';

var maskLen = function (data) {
function maskLen(data) {
var i;
var str = '';
for (i = 0; i < data.length; i++) {
str += '*';
}
return str;
};
}

var maskHalf = function (data) {
function maskHalf(data) {
var i;
var maskNum = data.length / 4;
data = data.split('');
Expand All @@ -18,45 +18,45 @@ var maskHalf = function (data) {
data[data.length - i - 1] = '*';
}
return data.join('');
};
}

var maskEvery = function (data) {
function maskEvery(data) {
var i;

data = data.split('');
for (i = 0; i < data.length / 3; i++) {
data[i * 3] = '*';
}
return data.join('');
};
}


var maskMiddle = function (data) {
function maskMiddle(data) {
var quarter = data.length / 4;
return data.substr(0, quarter) + '***' + data.substr(3 * quarter);
};
}

var maskSecure = function (data) {
function maskSecure() {
return '***';
};
}
/**
* @param data{string} string for masking
* @param [options]{Object} modes: secure (replace with "***"),
* length(replace with equal number of "*") and half (mask 1/4 of start and 1/4 of end);
*/

var mask = function (data, options) {
function mask(data, options) {
var maskMe = data;
if (typeof data !== 'string') {
maskMe = JSON.stringify(data);
}
if (data === null || data === undefined || data.length < 4) {
return maskSecure(data);
return maskSecure();
}
if (options === undefined) {
if (!options) {
options = {};
}
if (options.mode === undefined) {
if (!options.mode) {
options.mode = 'secure';
}

Expand All @@ -72,7 +72,7 @@ var mask = function (data, options) {
if (options.mode === 'middle') {
return maskMiddle(maskMe);
}
return maskSecure(maskMe);
};
return maskSecure();
}

module.exports = mask;
113 changes: 25 additions & 88 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,141 +1,78 @@
'use strict';

var mask = require('../script');
var assert = require('chai').assert;

describe('maskSensitive', function () {

it('should mask without params', function (done) {
it('should mask without params', function () {
var data = 'sensitive';
var masked = mask(data);
if (masked !== '***') {
done('not masked without params!');
} else done();
assert.equal(masked, '***');
});


it('should mask objects', function (done) {
it('should mask objects', function () {
var data = {smth: 'sensitive'};
var masked = mask(data);
if (masked !== '***') {
done('object not masked!');
} else done();
assert.equal(masked, '***');
});


it('should mask with length mode', function (done) {
it('should mask with length mode', function () {
var data = 'sensitive data';
var masked = mask(data, {mode: 'length'});
if (masked !== '**************') {
done('Not masked with length!');
} else done();
assert.equal(masked, '**************');
});


it('should mask with half mode', function (done) {
it('should mask with half mode', function () {
var data = 'sensitive data';
var masked = mask(data, {mode: 'half'});
if (masked !== '****itive ****') {
done('Not masked with half mode!');
} else {
done();
}

assert.equal(masked, '****itive ****');
});

it('should mask with every mode', function (done) {
it('should mask with every mode', function () {
var data = 'sensitive data';
var masked = mask(data, {mode: 'every'});
if (masked !== '*en*it*ve*da*a') {
done('Not masked with half mode!');
} else {
done();
}
assert.equal(masked, '*en*it*ve*da*a');
});


it('should mask with middle mode', function (done) {
it('should mask with middle mode', function () {
var data = 'sensitive data';
var masked = mask(data, {mode: 'middle'});
if (masked !== 'sen***data') {
done('Not masked with middle mode!');
} else {
done();
}
assert.equal(masked, 'sen***data');
});


it('should mask null values', function (done) {
it('should mask null values', function () {
var data = null;
var masked = mask(data, {mode: 'middle'});
if (masked !== '***') {
done('Not masked with middle mode! (result was ' + masked + ')');
return;
}
assert.equal(masked, '***');

masked = mask(data, {mode: 'every'});
if (masked !== '***') {
done('Not masked with every mode! (result was ' + masked + ')');
return;
}
assert.equal(masked, '***');
masked = mask(data, {mode: 'secure'});
if (masked !== '***') {
done('Not masked with secure mode! (result was ' + masked + ')');
return;
}
assert.equal(masked, '***');
masked = mask(data, {mode: 'half'});
if (masked !== '***') {
done('Not masked with half mode! (result was ' + masked + ')');
return;
}
assert.equal(masked, '***');
masked = mask(data, {mode: 'length'});
if (masked !== '***') {
done('Not masked with length mode! (result was ' + masked + ')');
return;
}
done();
});


it('should mask with middle mode', function (done) {
var data = 'sensitive data';
var masked = mask(data, {mode: 'middle'});
if (masked !== 'sen***data') {
done('Not masked with middle mode!');
} else {
done();
}
assert.equal(masked, '***');
});


it('should mask less then three chars with ***', function (done) {
it('should mask less then three chars with ***', function () {
var data = 'a';
var masked = mask(data, {mode: 'middle'});
if (masked !== '***') {
done('Not masked with middle mode! (result was ' + masked + ')');
return;
}

assert.equal(masked, '***');
masked = mask(data, {mode: 'every'});
if (masked !== '***') {
done('Not masked with every mode! (result was ' + masked + ')');
return;
}
assert.equal(masked, '***');
masked = mask(data, {mode: 'secure'});
if (masked !== '***') {
done('Not masked with secure mode! (result was ' + masked + ')');
return;
}
assert.equal(masked, '***');
masked = mask(data, {mode: 'half'});
if (masked !== '***') {
done('Not masked with half mode! (result was ' + masked + ')');
return;
}
assert.equal(masked, '***');
masked = mask(data, {mode: 'length'});
if (masked !== '***') {
done('Not masked with length mode! (result was ' + masked + ')');
return;
}
done();
assert.equal(masked, '***');
});


Expand Down

0 comments on commit 4a73d8f

Please sign in to comment.