Skip to content
This repository was archived by the owner on Apr 1, 2022. It is now read-only.

Commit c579c5c

Browse files
author
Ken Berkeley
committed
change hashCode algorithm
1 parent e034524 commit c579c5c

File tree

5 files changed

+33
-11
lines changed

5 files changed

+33
-11
lines changed

dist/min.css

Lines changed: 0 additions & 1 deletion
This file was deleted.

dist/min.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/HeaderSettings/index.vue

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,8 @@
4747
import ColumnGroup from './ColumnGroup.vue'
4848
import replaceWith from 'replace-with'
4949
import groupBy from 'lodash/groupBy'
50-
const LS = localStorage
51-
const parseStr = JSON.parse
52-
const stringify = JSON.stringify
53-
const rmFromLS = k => LS.removeItem(k)
54-
const saveToLS = (k, v) => LS.setItem(k, stringify(v))
55-
const getFromLS = k => { try { return parseStr(LS.getItem(k)) } catch (e) { rmFromLS(k) } }
56-
const hash = s => '' + s.split('').reduce((a, b) => (a = (a << 5) - a + b.charCodeAt(0), a & a), 0)
57-
// the hash algorithm above refers to http://stackoverflow.com/a/15710692/5172890
50+
import keyGen from '../_utils/keyGen'
51+
import { parseStr, stringify, saveToLS, rmFromLS, getFromLS } from '../_utils/localstorage'
5852
5953
export default {
6054
name: 'HeaderSettings',
@@ -69,7 +63,7 @@ export default {
6963
origSettings,
7064
usingBak: false, // is using backup
7165
processingCls: '',
72-
storageKey: this.supportBackup && hash(origSettings)
66+
storageKey: this.supportBackup && keyGen(origSettings)
7367
}
7468
},
7569
created () {

src/_utils/keyGen.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// https://stackoverflow.com/a/7616484/5172890
2+
export default function (s) {
3+
let hash = 0
4+
for (let i = 0; i < s.length; i++) {
5+
hash = ((hash << 5) - hash) + s.charCodeAt(i)
6+
hash |= 0
7+
}
8+
return hash
9+
}

src/_utils/localstorage.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const LS = localStorage
2+
3+
export const parseStr = JSON.parse
4+
5+
export const stringify = JSON.stringify
6+
7+
export const saveToLS = (k, v) => {
8+
LS.setItem(k, stringify(v))
9+
}
10+
11+
export const rmFromLS = k => {
12+
LS.removeItem(k)
13+
}
14+
15+
export const getFromLS = k => {
16+
try {
17+
return parseStr(LS.getItem(k))
18+
} catch (e) {
19+
rmFromLS(k)
20+
}
21+
}

0 commit comments

Comments
 (0)