Skip to content

Commit

Permalink
Hash computation performed one time, instead of for each conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Yakov Zhmurov committed Feb 7, 2018
1 parent c8893fd commit d0f2cb2
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ encodeURI(dumbson): -~name~*~John~.~value~*15!
This looks retarded, however:
- it gives better URLs, than just putting plain or base64 JSON into query string
- it works as reliable as plain JSON
- there is strong guarantee that ```serialize(parse(dumbson)) = dumbson```
- ```serialize(parse(dumbson)) == dumbson```, which hard to guarantee for more complex methods

## Usage

Expand Down
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ var replacements = {
to: '()-!*.~'
};

var hash = {};
replacements.from.split('').forEach((c, n) => hash[c] = replacements.to[n]);
replacements.to.split('').forEach((c, n) => hash[c] = replacements.from[n]);

function replace(src) {
src = src || "";
var hash = {};
replacements.from.split('').forEach((c, n) => hash[c] = replacements.to[n]);
replacements.to.split('').forEach((c, n) => hash[c] = replacements.from[n]);

var chars = src.split('');
chars.forEach((c, n) => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dumbson",
"version": "0.1.0",
"version": "0.1.2",
"description": "A dumb way to serialize JS objects to query string",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit d0f2cb2

Please sign in to comment.