summaryrefslogtreecommitdiff
path: root/@linaria/packages/babel/src/utils/slugify.ts
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2021-08-23 16:46:06 -0300
committerSebastian <sebasjm@gmail.com>2021-08-23 16:48:30 -0300
commit38acabfa6089ab8ac469c12b5f55022fb96935e5 (patch)
tree453dbf70000cc5e338b06201af1eaca8343f8f73 /@linaria/packages/babel/src/utils/slugify.ts
parentf26125e039143b92dc0d84e7775f508ab0cdcaa8 (diff)
downloadnode-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.tar.gz
node-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.tar.bz2
node-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.zip
added web vendorsHEADmaster
Diffstat (limited to '@linaria/packages/babel/src/utils/slugify.ts')
-rw-r--r--@linaria/packages/babel/src/utils/slugify.ts83
1 files changed, 83 insertions, 0 deletions
diff --git a/@linaria/packages/babel/src/utils/slugify.ts b/@linaria/packages/babel/src/utils/slugify.ts
new file mode 100644
index 0000000..39772ad
--- /dev/null
+++ b/@linaria/packages/babel/src/utils/slugify.ts
@@ -0,0 +1,83 @@
+/**
+ * This file contains a utility to generate hashes to be used as generated class names
+ */
+
+/* eslint-disable no-bitwise, default-case, no-param-reassign, prefer-destructuring */
+
+/**
+ * murmurhash2 via https://gist.github.com/raycmorgan/588423
+ */
+
+function doHash(str: string, seed: number = 0) {
+ const m = 0x5bd1e995;
+ const r = 24;
+ let h = seed ^ str.length;
+ let length = str.length;
+ let currentIndex = 0;
+
+ while (length >= 4) {
+ let k = UInt32(str, currentIndex);
+
+ k = Umul32(k, m);
+ k ^= k >>> r;
+ k = Umul32(k, m);
+
+ h = Umul32(h, m);
+ h ^= k;
+
+ currentIndex += 4;
+ length -= 4;
+ }
+
+ switch (length) {
+ case 3:
+ h ^= UInt16(str, currentIndex);
+ h ^= str.charCodeAt(currentIndex + 2) << 16;
+ h = Umul32(h, m);
+ break;
+
+ case 2:
+ h ^= UInt16(str, currentIndex);
+ h = Umul32(h, m);
+ break;
+
+ case 1:
+ h ^= str.charCodeAt(currentIndex);
+ h = Umul32(h, m);
+ break;
+ }
+
+ h ^= h >>> 13;
+ h = Umul32(h, m);
+ h ^= h >>> 15;
+
+ return h >>> 0;
+}
+
+function UInt32(str: string, pos: number) {
+ return (
+ str.charCodeAt(pos++) +
+ (str.charCodeAt(pos++) << 8) +
+ (str.charCodeAt(pos++) << 16) +
+ (str.charCodeAt(pos) << 24)
+ );
+}
+
+function UInt16(str: string, pos: number) {
+ return str.charCodeAt(pos++) + (str.charCodeAt(pos++) << 8);
+}
+
+function Umul32(n: number, m: number) {
+ n |= 0;
+ m |= 0;
+ const nlo = n & 0xffff;
+ const nhi = n >>> 16;
+ const res = (nlo * m + (((nhi * m) & 0xffff) << 16)) | 0;
+ return res;
+}
+
+function slugify(code: string) {
+ return doHash(code).toString(36);
+}
+
+export default slugify;