Skip to content

Commit c989011

Browse files
committed
Add befor/after decorations
1 parent afbe654 commit c989011

12 files changed

Lines changed: 565 additions & 90 deletions

File tree

src/vs/base/common/hash.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
'use strict';
6+
7+
/**
8+
* Return a hash value for an object.
9+
*/
10+
export function hash(obj: any, hashVal = 0) : number {
11+
switch (typeof obj) {
12+
case 'object':
13+
if (obj === null) {
14+
return numberHash(349, hashVal);
15+
} else if (Array.isArray(obj)) {
16+
return arrayHash(obj, hashVal);
17+
}
18+
return objectHash(obj, hashVal);
19+
case 'string':
20+
return stringHash(obj, hashVal);
21+
case 'boolean':
22+
return booleanHash(obj, hashVal);
23+
case 'number':
24+
return numberHash(obj, hashVal);
25+
case 'undefined':
26+
return numberHash(obj, 937);
27+
default:
28+
return numberHash(obj, 617);
29+
}
30+
}
31+
32+
function numberHash(val: number, initialHashVal: number) : number {
33+
return (((initialHashVal << 5) - initialHashVal) + val) | 0; // hashVal * 31 + ch, keep as int32
34+
}
35+
36+
function booleanHash(b: boolean, initialHashVal: number) : number {
37+
return numberHash(b ? 433 : 863, initialHashVal);
38+
}
39+
40+
function stringHash(s: string, hashVal: number) {
41+
hashVal = numberHash(149417, hashVal);
42+
for (let i = 0, length = s.length; i < length; i++) {
43+
hashVal = numberHash(s.charCodeAt(i), hashVal);
44+
}
45+
return hashVal;
46+
}
47+
48+
function arrayHash(arr: any[], initialHashVal: number) : number {
49+
initialHashVal = numberHash(104579, initialHashVal);
50+
return arr.reduce((hashVal, item) => hash(item, hashVal), initialHashVal);
51+
}
52+
53+
function objectHash(obj: any, initialHashVal: number) : number {
54+
initialHashVal = numberHash(181387, initialHashVal);
55+
return Object.keys(obj).sort().reduce((hashVal, key) => {
56+
hashVal = stringHash(key, hashVal);
57+
return hash(obj[key], hashVal);
58+
}, initialHashVal);
59+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
'use strict';
6+
7+
import * as assert from 'assert';
8+
import {hash} from 'vs/base/common/hash';
9+
10+
suite('Hash', () => {
11+
test('string', () => {
12+
assert.equal(hash('hello'), hash('hello'));
13+
assert.notEqual(hash('hello'), hash('world'));
14+
assert.notEqual(hash('hello'), hash('olleh'));
15+
assert.notEqual(hash('hello'), hash('Hello'));
16+
assert.notEqual(hash('hello'), hash('Hello '));
17+
assert.notEqual(hash('h'), hash('H'));
18+
assert.notEqual(hash('-'), hash('_'));
19+
});
20+
21+
test('number', () => {
22+
assert.equal(hash(1), hash(1.0));
23+
assert.notEqual(hash(0), hash(1));
24+
assert.notEqual(hash(1), hash(-1));
25+
assert.notEqual(hash(0x12345678), hash(0x123456789));
26+
});
27+
28+
test('boolean', () => {
29+
assert.equal(hash(true), hash(true));
30+
assert.notEqual(hash(true), hash(false));
31+
});
32+
33+
test('array', () => {
34+
assert.equal(hash([1, 2, 3]), hash([1, 2, 3]));
35+
assert.equal(hash(['foo', 'bar']), hash(['foo', 'bar']));
36+
assert.equal(hash([]), hash([]));
37+
assert.notEqual(hash(['foo', 'bar']), hash(['bar', 'foo']));
38+
assert.notEqual(hash(['foo', 'bar']), hash(['bar', 'foo', null]));
39+
});
40+
41+
test('object', () => {
42+
assert.equal(hash({}), hash({}));
43+
assert.equal(hash({'foo': 'bar'}), hash({'foo': 'bar'}));
44+
assert.equal(hash({'foo': 'bar', 'foo2': void 0}), hash({'foo2': void 0, 'foo': 'bar'}));
45+
assert.notEqual(hash({'foo': 'bar'}), hash({'foo': 'bar2'}));
46+
assert.notEqual(hash({}), hash([]));
47+
});
48+
});

0 commit comments

Comments
 (0)