├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── example.js ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '6' 4 | - '8' 5 | - '10' 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Mathias Buus 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # xor-distance 2 | 3 | Calculate the xor distance between two buffers as a new buffer and compare computed distances with eachother 4 | 5 | ``` 6 | npm install xor-distance 7 | ``` 8 | 9 | [![build status](http://img.shields.io/travis/mafintosh/xor-distance.svg?style=flat)](http://travis-ci.org/mafintosh/xor-distance) 10 | 11 | ## Usage 12 | 13 | ``` js 14 | var distance = require('./') 15 | 16 | var dist1 = distance(new Buffer('foo'), new Buffer('bar')) 17 | var dist2 = distance(new Buffer('foo'), new Buffer('baz')) 18 | 19 | // the following returns true since the distance between foo and bar 20 | // is greater than the distance between foo and baz 21 | console.log(distance.gt(dist1, dist2)) 22 | ``` 23 | 24 | ## License 25 | 26 | MIT 27 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | var distance = require('./') 2 | 3 | var dist1 = distance(new Buffer('foo'), new Buffer('bar')) 4 | var dist2 = distance(new Buffer('foo'), new Buffer('baz')) 5 | 6 | console.log(distance.gt(dist1, dist2)) 7 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = dist 2 | 3 | function dist (a, b) { 4 | if (a.length !== b.length) throw new Error('Inputs should have the same length') 5 | var result = Buffer.allocUnsafe(a.length) 6 | for (var i = 0; i < a.length; i++) result[i] = a[i] ^ b[i] 7 | return result 8 | } 9 | 10 | dist.compare = function compare (a, b) { 11 | if (a.length !== b.length) throw new Error('Inputs should have the same length') 12 | for (var i = 0; i < a.length; i++) { 13 | if (a[i] === b[i]) continue 14 | return a[i] < b[i] ? -1 : 1 15 | } 16 | return 0 17 | } 18 | 19 | dist.gt = function gt (a, b) { 20 | return dist.compare(a, b) === 1 21 | } 22 | 23 | dist.lt = function lt (a, b) { 24 | return dist.compare(a, b) === -1 25 | } 26 | 27 | dist.eq = function eq (a, b) { 28 | return dist.compare(a, b) === 0 29 | } 30 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "xor-distance", 3 | "version": "2.0.0", 4 | "description": "Calculate the distance between two buffers as a new buffer and compare computed distances with eachother", 5 | "main": "index.js", 6 | "dependencies": {}, 7 | "devDependencies": { 8 | "tape": "^4.2.0" 9 | }, 10 | "scripts": { 11 | "test": "tape test.js" 12 | }, 13 | "repository": { 14 | "type": "git", 15 | "url": "https://github.com/mafintosh/xor-distance.git" 16 | }, 17 | "author": "Mathias Buus (@mafintosh)", 18 | "license": "MIT", 19 | "bugs": { 20 | "url": "https://github.com/mafintosh/xor-distance/issues" 21 | }, 22 | "homepage": "https://github.com/mafintosh/xor-distance" 23 | } 24 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var tape = require('tape') 2 | var distance = require('./') 3 | 4 | tape('distance', function (t) { 5 | t.same(distance(Buffer.from([1, 0]), Buffer.from([0, 1])), Buffer.from([1, 1])) 6 | t.same(distance(Buffer.from([1, 1]), Buffer.from([0, 1])), Buffer.from([1, 0])) 7 | t.same(distance(Buffer.from([1, 1]), Buffer.from([1, 1])), Buffer.from([0, 0])) 8 | t.end() 9 | }) 10 | 11 | tape('compare', function (t) { 12 | t.same(distance.compare(Buffer.from([0, 0]), Buffer.from([0, 1])), -1) 13 | t.same(distance.compare(Buffer.from([0, 1]), Buffer.from([0, 1])), 0) 14 | t.same(distance.compare(Buffer.from([1, 1]), Buffer.from([0, 1])), 1) 15 | t.end() 16 | }) 17 | 18 | tape('shorthands', function (t) { 19 | t.ok(distance.lt(Buffer.from([0, 0]), Buffer.from([0, 1]))) 20 | t.ok(distance.eq(Buffer.from([0, 1]), Buffer.from([0, 1]))) 21 | t.ok(distance.gt(Buffer.from([1, 1]), Buffer.from([0, 1]))) 22 | t.end() 23 | }) 24 | --------------------------------------------------------------------------------