Created
January 2, 2014 19:19
-
-
Save icodeforlove/8224874 to your computer and use it in GitHub Desktop.
cdn url generator
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
function CDN ($config) { | |
this.protocol = $config.protocol; | |
this.domain = $config.domain; | |
this.alias = $config.alias; | |
this.total = $config.total; | |
} | |
CDN.prototype = { | |
getURL: function (path) { | |
return this.protocol + '://' + this.alias + (this._stringToNumber(path) % this.total) + '.' + this.domain + path; | |
}, | |
_stringToNumber: function (string) { | |
return string | |
.split('') | |
.map(function (value) { | |
return value.charCodeAt(0); | |
}) | |
.reduce(function (a, b) { | |
return a + b; | |
}, 0); | |
} | |
}; | |
var cdn = new CDN({ | |
protocol: 'http', | |
domain: 'google.com', | |
alias: 'cdn', | |
total: 5 | |
}); | |
console.log(cdn.getURL('/0cc175b9c0f1b6a831c399e269772661.jpeg')); | |
console.log(cdn.getURL('/9e3669d19b675bd57058fd4664205d2a.jpeg')); | |
console.log(cdn.getURL('/4a8a08f09d37b73795649038408b5f33.jpeg')); | |
console.log(cdn.getURL('/8277e0910d750195b448797616e091ad.jpeg')); | |
console.log(cdn.getURL('/b798abe6e1b1318ee36b0dcb3fb9e4d3.jpeg')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
jj