Skip to content

Commit 482317d

Browse files
committed
replacing md5 with simple hash; misc cleanup
1 parent ac1ada5 commit 482317d

File tree

5 files changed

+41
-38
lines changed

5 files changed

+41
-38
lines changed

package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
"peerDependencies": {
1818
"dustjs-linkedin": "^2.7.5"
1919
},
20-
"dependencies": {
21-
"md5": "^2.2.1"
22-
},
2320
"devDependencies": {
2421
"dustjs-linkedin": "^2.7.5",
2522
"rollup": "^0.41.6",

rollup.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ export default {
44
format: 'cjs',
55
external: [
66
'dustjs-linkedin',
7-
'md5',
87
'path'
98
]
109
}

src/index.js

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,18 @@
1-
import md5 from 'md5'
21
import dust, { compile } from 'dustjs-linkedin'
3-
import { basename, dirname, extname, resolve } from 'path'
4-
5-
const { floor, random } = Math
6-
const token = floor(random() * 10000000)
7-
8-
function getFullPath (from, to) {
9-
return resolve(dirname(from), to)
10-
}
11-
12-
function generateName (path) {
13-
const name = basename(path, '.dust')
14-
const hash = md5(token + path)
15-
16-
return `${name}-${hash}`
17-
}
2+
import { dirname, extname, resolve } from 'path'
3+
import { generateName, hash } from './utils.js'
184

5+
/**
6+
* Finds all valid partials and replaces paths with respective
7+
* unique template names
8+
*/
199
function resolvePartials (source, parentPath) {
2010
const partials = []
11+
// matches {>"./file.dust" /}
2112
const partialRegex = /\{>\s*"([^"]+)"[^\n]*\/\}/g
2213
const newSource = source.replace(partialRegex, (tag, relPath) => {
2314
if (extname(relPath) === '.dust') {
24-
const fullPath = getFullPath(parentPath, relPath)
15+
const fullPath = resolve(dirname(parentPath), relPath)
2516
const name = generateName(fullPath)
2617

2718
partials.push(relPath)

src/utils.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { basename } from 'path'
2+
3+
const { abs, floor, random } = Math
4+
const token = floor(random() * 10000000)
5+
6+
/*
7+
* http://werxltd.com/wp/2010/05/13/javascript-implementation-of-javas-string-hashcode-method/
8+
*/
9+
export function hash (input) {
10+
let hash = 0
11+
12+
if (input.length) {
13+
for (let i = 0; i < input.length; i++) {
14+
const char = input.charCodeAt(i)
15+
16+
hash = ((hash << 5) - hash) + char
17+
hash = hash & hash
18+
}
19+
}
20+
21+
return abs(hash)
22+
}
23+
24+
/**
25+
* Generates unique template name for the provided path
26+
*/
27+
export function generateName (path) {
28+
const name = basename(path, '.dust')
29+
const pathHash = hash(token + path)
30+
31+
return `${name}-${pathHash}`
32+
}

yarn.lock

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,6 @@ caseless@~0.12.0:
130130
version "0.12.0"
131131
resolved "https://registry.yarnpkg.com/caseless/-/caseless-0.12.0.tgz#1b681c21ff84033c826543090689420d187151dc"
132132

133-
charenc@~0.0.1:
134-
version "0.0.2"
135-
resolved "https://registry.yarnpkg.com/charenc/-/charenc-0.0.2.tgz#c0a1d2f3a7092e03774bfa83f14c0fc5790a8667"
136-
137133
chokidar@~1.5.1:
138134
version "1.5.2"
139135
resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-1.5.2.tgz#293e728640cc93dd8277424334b3c6d4ad3a348a"
@@ -182,10 +178,6 @@ core-util-is@~1.0.0:
182178
version "1.0.2"
183179
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
184180

185-
crypt@~0.0.1:
186-
version "0.0.2"
187-
resolved "https://registry.yarnpkg.com/crypt/-/crypt-0.0.2.tgz#88d7ff7ec0dfb86f713dc87bbb42d044d3e6c41b"
188-
189181
cryptiles@2.x.x:
190182
version "2.0.5"
191183
resolved "https://registry.yarnpkg.com/cryptiles/-/cryptiles-2.0.5.tgz#3bdfecdc608147c1c67202fa291e7dca59eaa3b8"
@@ -427,7 +419,7 @@ is-binary-path@^1.0.0:
427419
dependencies:
428420
binary-extensions "^1.0.0"
429421

430-
is-buffer@^1.0.2, is-buffer@~1.1.1:
422+
is-buffer@^1.0.2:
431423
version "1.1.5"
432424
resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-1.1.5.tgz#1f3b26ef613b214b88cbca23cc6c01d87961eecc"
433425

@@ -536,14 +528,6 @@ kind-of@^3.0.2:
536528
dependencies:
537529
is-buffer "^1.0.2"
538530

539-
md5@^2.2.1:
540-
version "2.2.1"
541-
resolved "https://registry.yarnpkg.com/md5/-/md5-2.2.1.tgz#53ab38d5fe3c8891ba465329ea23fac0540126f9"
542-
dependencies:
543-
charenc "~0.0.1"
544-
crypt "~0.0.1"
545-
is-buffer "~1.1.1"
546-
547531
micromatch@^2.1.5:
548532
version "2.3.11"
549533
resolved "https://registry.yarnpkg.com/micromatch/-/micromatch-2.3.11.tgz#86677c97d1720b363431d04d0d15293bd38c1565"

0 commit comments

Comments
 (0)