Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
87b5672
commit b634925
Showing
5 changed files
with
136 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { hashString } from "../hash"; | ||
|
||
describe("hash", () => { | ||
it("should calc hash", () => { | ||
const text = ` | ||
*[data-zstyl='id'] { display: flex; justify-content: center; color: green; } | ||
@media (max-width: 600px) { | ||
*[data-zstyl='id'] { display: inline-flex; } | ||
*[data-zstyl='id']:hover { color: black; } | ||
} | ||
*[data-zstyl='id']:hover { color: red; } | ||
*[data-zstyl='id']:active { color: blue; } | ||
*[data-zstyl='id'] .hoge { width: 100%; } | ||
`.trim(); | ||
const actual = hashString(text); | ||
expect(actual).toBe("bJIaaS"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const AD_REPLACER_R = /(a)(d)/gi; | ||
|
||
const charsLength = 52; | ||
function getAlphabeticChar(code: number) { | ||
return String.fromCharCode(code + (code > 25 ? 39 : 97)); | ||
} | ||
|
||
function getAlphabeticString(code: number) { | ||
let name = ''; | ||
let x; | ||
|
||
for (x = Math.abs(code); x > charsLength; x = (x / charsLength) | 0) { | ||
name = getAlphabeticChar(x % charsLength) + name; | ||
} | ||
|
||
return (getAlphabeticChar(x % charsLength) + name).replace(AD_REPLACER_R, "$1-$2"); | ||
} | ||
|
||
// djb2 algorithm | ||
function phash(h: number, text: string) { | ||
let i = text.length; | ||
while(i) { | ||
h = (h * 33) ^ text.charCodeAt(--i); | ||
} | ||
return h; | ||
} | ||
|
||
const SEED = 5381; | ||
|
||
export function hashString(text: string) { | ||
return getAlphabeticString(phash(SEED, text)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters