Skip to content

Commit

Permalink
Switch away from BigNumber.js (#497)
Browse files Browse the repository at this point in the history
* test hash function

* need max lengths, found a test case

* anybase is much smaller. using every testing image I could not find one that needed to be padded. small or large.
  • Loading branch information
hipstersmoothie committed Jul 30, 2018
1 parent fb87f39 commit c488bff
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"author": "Oliver Moran <oliver.moran@gmail.com>",
"license": "MIT",
"dependencies": {
"bignumber.js": "^7.2.1",
"any-base": "^1.1.0",
"bmp-js": "0.1.0",
"buffer": "^5.2.0",
"exif-parser": "^0.1.12",
Expand Down
21 changes: 11 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import FS from 'fs';
import Path from 'path';
import EventEmitter from 'events';

import { BigNumber } from 'bignumber.js';
import anyBase from 'any-base';
import bMFont from 'load-bmfont';
import MkDirP from 'mkdirp';
import pixelMatch from 'pixelmatch';
Expand All @@ -29,17 +29,18 @@ if (
require('source-map-support').install();
}

BigNumber.set({
ALPHABET: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_'
});
const alphabet =
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_';

// an array storing the maximum string length of hashes at various bases
const maxHashLength = [];
// 0 and 1 do not exist as possible hash lengths
const maxHashLength = [NaN, NaN];

for (let i = 0; i < 65; i++) {
const l =
i > 1 ? new BigNumber(new Array(64 + 1).join('1'), 2).toString(i) : NaN;
maxHashLength.push(l.length);
for (let i = 2; i < 65; i++) {
const maxHash = anyBase(anyBase.BIN, alphabet.slice(0, i))(
new Array(64 + 1).join('1')
);
maxHashLength.push(maxHash.length);
}

process.on('exit', clear);
Expand Down Expand Up @@ -615,7 +616,7 @@ class Jimp extends EventEmitter {
}

let hash = new ImagePHash().getHash(this);
hash = new BigNumber(hash, 2).toString(base);
hash = anyBase(anyBase.BIN, alphabet.slice(0, base))(hash);

while (hash.length < maxHashLength[base]) {
hash = '0' + hash; // pad out with leading zeros
Expand Down
62 changes: 62 additions & 0 deletions test/hash.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/* eslint-disable no-control-regex */

const { Jimp, getTestDir } = require('./test-helper');

describe.only('hash', () => {
const imagesDir = getTestDir() + '/samples';

it('base 2', done => {
new Jimp(imagesDir + '/dice.png', (err, image) => {
if (err) done(err);
image
.hash(2)
.should.be.equal(
'1100010000011111011010111110000000010101001011010101101000010010'
);
done();
});
});

it('base 10 (decimal)', done => {
new Jimp(imagesDir + '/cops.jpg', (err, image) => {
if (err) done(err);
image.hash(10).should.be.equal('13442314021806033441');
done();
});
});

it('base 16 (hex)', done => {
new Jimp(imagesDir + '/rgb.tiff', (err, image) => {
if (err) done(err);
image.hash(16).should.be.equal('949800481007044c');
done();
});
});

it('base 64', done => {
new Jimp(imagesDir + '/windows95.bmp', (err, image) => {
if (err) done(err);
image.hash(64).should.be.equal('f30wi0ww000');
done();
});
});

it('base 23', function(done) {
// large image need large timeout, but this really seems to be an issue
// with should. If I change the expected value it will complete quicker! :(
this.timeout(3000);
new Jimp(imagesDir + '/panoramic.jpg', (err, image) => {
if (err) done(err);
image.hash(23).should.be.exactly('0m1m2id7l7cl4fb');
done();
});
});

it('base 17', done => {
new Jimp(imagesDir + '/lenna.png', (err, image) => {
if (err) done(err);
image.hash(17).should.be.equal('4fa6aga5a64ad0c1');
done();
});
});
});
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,10 @@ ansi-styles@^3.2.0, ansi-styles@^3.2.1:
dependencies:
color-convert "^1.9.0"

any-base@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/any-base/-/any-base-1.1.0.tgz#ae101a62bc08a597b4c9ab5b7089d456630549fe"

any-observable@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/any-observable/-/any-observable-0.3.0.tgz#af933475e5806a67d0d7df090dd5e8bef65d119b"
Expand Down Expand Up @@ -1233,10 +1237,6 @@ better-assert@~1.0.0:
dependencies:
callsite "1.0.0"

bignumber.js@^7.2.1:
version "7.2.1"
resolved "https://registry.yarnpkg.com/bignumber.js/-/bignumber.js-7.2.1.tgz#80c048759d826800807c4bfd521e50edbba57a5f"

binary-extensions@^1.0.0:
version "1.11.0"
resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-1.11.0.tgz#46aa1751fb6a2f93ee5e689bb1087d4b14c6c205"
Expand Down

0 comments on commit c488bff

Please sign in to comment.