Comparing version 0.1.1 to 0.1.2
{ | ||
"name": "anonymizer", | ||
"description": "An easy way to anonymize your json. This utility can remove all field names from javascript objects", | ||
"version": "0.1.1", | ||
"description": "An easy way to anonymize your json. This utility can remove all field names from javascript objects.", | ||
"version": "0.1.2", | ||
"main": "src/anonymizer.js", | ||
@@ -6,0 +6,0 @@ "devDependencies": { |
110
README.md
# anonymizer | ||
[![Build Status](https://secure.travis-ci.org/user/anonymizer.png?branch=master)](http://travis-ci.org/user/anonymizer) | ||
[![Build Status](https://secure.travis-ci.org/kristw/anonymizer.png?branch=master)](http://travis-ci.org/kristw/anonymizer) | ||
Let's say you have to put a dataset on your website for your own use, but don't really want to share it to everyone. If you upload the raw, nice dataset with all the field names, people can just take it for their own use easily. You want to make it a bit harder for them to interpret the data without shooting yourself in the foot. | ||
## Installation | ||
This **anonymizer** was built for that purpose: | ||
* Given a schema, it can easily strip out all the field names. | ||
* It works with nested schema, e.g., Array of Objects, Array of Objects that contains Array, and so on. | ||
* It can also take all categorical values and map them into integers, which are more compact and help you anonymize the data. | ||
The library is also very lightweight (raw 3KB, minified 1KB) and supports CommonJS, AMD (RequireJS) as well as browser use. | ||
## 1. Installation | ||
Install with npm: | ||
@@ -14,11 +23,17 @@ | ||
## Usage | ||
or bower | ||
``` | ||
bower install --save anonymizer | ||
``` | ||
## 2. Usage | ||
``` | ||
var anonymizer = new Anonymizer(); | ||
``` | ||
### Basic | ||
### 2.1 Basic | ||
#### anonymizer.encode(data [,schema]) | ||
#### anonymizer.encode(data, schema) | ||
``` | ||
@@ -38,3 +53,3 @@ anonymizer.encode( | ||
#### anonymizer.decode(data [,schema]) | ||
#### anonymizer.decode(data, schema) | ||
``` | ||
@@ -54,3 +69,3 @@ anonymizer.decode( | ||
### Categorical values | ||
### 2.2 Categorical values | ||
@@ -63,3 +78,3 @@ Anonymizer can map categorical values to/from integers. | ||
anonymizer.encode(['test', 'test2', 'test'], 'Category'); | ||
anonymizer.encode(['test', 'test2', 'test'], ['Category']); | ||
=> [1, 2, 1] | ||
@@ -80,4 +95,81 @@ | ||
## Testing | ||
## 3. Schema definition | ||
**Primitive values** | ||
Use string | ||
``` | ||
2 | ||
=> schema = 'Number' | ||
'test value' | ||
=> 'String' | ||
true | ||
=> schema = 'Boolean' | ||
``` | ||
**Categorical values** | ||
Use string "Categorical" | ||
``` | ||
'category1' | ||
=> schema = 'Categorical' | ||
``` | ||
**Object** | ||
Use curly braces and contain all the keys that you want to export. The value for each key is the type for that field. | ||
``` | ||
{a: 1, b:'test', c: true} | ||
=> schema = {a: 'Number', b: 'String', c: 'Boolean'} | ||
``` | ||
**Array** | ||
Use square brackets and contains the type of an element. This library assume that all children of an array are of the same type, so you will define the schema for the child only once. | ||
``` | ||
[1, 2, 3] | ||
=> schema = ['Number'] | ||
['test1', 'test2'] | ||
=> schema = ['String'] | ||
['category1', 'category2'] | ||
=> schema = ['Category'] | ||
``` | ||
**Nesting** | ||
You can combine the syntax above to define any schema for you data. | ||
Array of objects | ||
``` | ||
[ | ||
{a: 1, b: true, c: 'test'}, | ||
{a: 2, b: false, c: 'test2'} | ||
] | ||
=> schema = [{a: 'Number', b: 'Boolean', c: 'String'}] | ||
``` | ||
Array of arrays | ||
``` | ||
[ | ||
[1, 2, 3], | ||
['a', 'b', 'c'] | ||
] | ||
=> schema = [['Number'], ['String']] | ||
``` | ||
Object that contains an array | ||
``` | ||
{ | ||
a: [1, 2, 3], | ||
b: ['a','b','c'], | ||
c: 'test' | ||
} | ||
=> schema = {a: ['Number'], b: ['String'], c: 'String'} | ||
``` | ||
## 4. Testing | ||
From the repo root: | ||
@@ -84,0 +176,0 @@ |
@@ -86,5 +86,5 @@ 'use strict'; | ||
if(isArray(data)){ | ||
var childSchema = isArray(schema) && schema.length > 0 ? schema[0] : null; | ||
return data.map(function(row){ | ||
return self.encode(row, childSchema); | ||
var childSchema = (schema.length > 0) ? schema[0] : null; | ||
return data.map(function(row, i){ | ||
return self.encode(row, i<schema.length ? schema[i] : childSchema); | ||
}); | ||
@@ -98,5 +98,8 @@ } | ||
} | ||
else if(schema==='Category'){ | ||
else if(schema==='Category' || schema==='category'){ | ||
return this.getCategoryCode(data); | ||
} | ||
else if(schema==='Number' || schema==='number'){ | ||
return +data; | ||
} | ||
else{ | ||
@@ -111,5 +114,5 @@ return data; | ||
if(isArray(schema)){ | ||
var childSchema = (isArray(schema) && schema.length > 0) ? schema[0] : null; | ||
return data.map(function(row){ | ||
return self.decode(row, childSchema); | ||
var childSchema = (schema.length > 0) ? schema[0] : null; | ||
return data.map(function(row, i){ | ||
return self.decode(row, i<schema.length ? schema[i] : childSchema); | ||
}); | ||
@@ -123,3 +126,3 @@ } | ||
} | ||
else if(schema==='Category'){ | ||
else if(schema==='Category' || schema==='category'){ | ||
return this.getCategoryName(data); | ||
@@ -126,0 +129,0 @@ } |
@@ -1,1 +0,1 @@ | ||
"use strict";!function(e,t){"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?module.exports=t():e.anonymizer=t()}(this,function(){function e(e){t(e)?(this.categories=e,this.categoryLookup=e.reduce(function(e,t,r){return e[t]=r,e},{})):(this.categories=[],this.categoryLookup={})}var t=Array.isArray||function(e){return"[object Array]"==Object.prototype.toString.call(e)},r=function(e){return"object"==typeof e&&null!==e},o=e.prototype;return o.getCategoryCode=function(e){return this.categoryLookup.hasOwnProperty(e)?this.categoryLookup[e]:(this.categories.push(e),this.categoryLookup[e]=this.categories.length,this.categories.length)},o.getCategoryName=function(e){return e>this.categories.length?null:this.categories[e-1]},o.getCategories=function(){return this.categories},o.encode=function(e,o){var n=this;if(t(e)){var i=t(o)&&o.length>0?o[0]:null;return e.map(function(e){return n.encode(e,i)})}if(r(e)){if(!r(o))throw"Expect schema to be an object but receive: "+JSON.stringify(o);return Object.keys(o).map(function(t){return n.encode(e[t],o[t])})}return"Category"===o?this.getCategoryCode(e):e},o.decode=function(e,o){var n=this;if(t(o)){var i=t(o)&&o.length>0?o[0]:null;return e.map(function(e){return n.decode(e,i)})}return r(o)?Object.keys(o).reduce(function(t,r,i){return t[r]=n.decode(e[i],o[r]),t},{}):"Category"===o?this.getCategoryName(e):e},e}); | ||
"use strict";!function(e,t){"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?module.exports=t():e.anonymizer=t()}(this,function(){function e(e){t(e)?(this.categories=e,this.categoryLookup=e.reduce(function(e,t,r){return e[t]=r,e},{})):(this.categories=[],this.categoryLookup={})}var t=Array.isArray||function(e){return"[object Array]"==Object.prototype.toString.call(e)},r=function(e){return"object"==typeof e&&null!==e},o=e.prototype;return o.getCategoryCode=function(e){return this.categoryLookup.hasOwnProperty(e)?this.categoryLookup[e]:(this.categories.push(e),this.categoryLookup[e]=this.categories.length,this.categories.length)},o.getCategoryName=function(e){return e>this.categories.length?null:this.categories[e-1]},o.getCategories=function(){return this.categories},o.encode=function(e,o){var n=this;if(t(e)){var i=o.length>0?o[0]:null;return e.map(function(e,t){return n.encode(e,t<o.length?o[t]:i)})}if(r(e)){if(!r(o))throw"Expect schema to be an object but receive: "+JSON.stringify(o);return Object.keys(o).map(function(t){return n.encode(e[t],o[t])})}return"Category"===o||"category"===o?this.getCategoryCode(e):"Number"===o||"number"===o?+e:e},o.decode=function(e,o){var n=this;if(t(o)){var i=o.length>0?o[0]:null;return e.map(function(e,t){return n.decode(e,t<o.length?o[t]:i)})}return r(o)?Object.keys(o).reduce(function(t,r,i){return t[r]=n.decode(e[i],o[r]),t},{}):"Category"===o||"category"===o?this.getCategoryName(e):e},e}); |
@@ -8,3 +8,3 @@ 'use strict'; | ||
describe('#encode(data [,schema])', function(){ | ||
describe('#encode(data, schema)', function(){ | ||
it('can encode primitive values', function () { | ||
@@ -29,2 +29,15 @@ var anonymizer = new Anonymizer(); | ||
it('can encode string', function () { | ||
var anonymizer = new Anonymizer(); | ||
expect(anonymizer.encode('abc', 'String')).to.equal('abc'); | ||
}); | ||
it('can encode number', function () { | ||
var anonymizer = new Anonymizer(); | ||
expect(anonymizer.encode('1', 'Number')).to.equal(1); | ||
expect(anonymizer.encode('11.11', 'number')).to.equal(11.11); | ||
}); | ||
it('can encode categorical values', function () { | ||
@@ -34,2 +47,3 @@ var anonymizer = new Anonymizer(); | ||
expect(anonymizer.encode('a', 'Category')).to.equal(1); | ||
expect(anonymizer.encode('a', 'category')).to.equal(1); | ||
expect(anonymizer.encode(['a','b','a'], ['Category'])).to.deep.equal([1, 2, 1]); | ||
@@ -144,3 +158,3 @@ }); | ||
describe('#decode(data [,schema])', function(){ | ||
describe('#decode(data, schema)', function(){ | ||
it('can decode primitive values', function () { | ||
@@ -147,0 +161,0 @@ var anonymizer = new Anonymizer(); |
19028
11
372
176