@procore/cdn
Advanced tools
Comparing version 1.0.2 to 2.0.0
const cdn = require('./../cdn'); | ||
var asset1 = 'https://assset%d.cdn.com/test.png'; | ||
var asset2 = 'https://assset%d.cdn.com/test.jpg'; | ||
var asset3 = 'https://assset%d.cdn.com/test.jpg?size=sm'; | ||
var asset4 = 'https://assset%d.cdn.com/test.png#sm'; | ||
var variants = 3; | ||
var memoizedCdn = cdn(variants); | ||
test('increments the count', () => { | ||
const src = 'https://assset%d.cdn.com/test.png' | ||
var variants = 5; | ||
expect(memoizedCdn(asset1)).toBe('https://assset0.cdn.com/test.png'); | ||
expect(memoizedCdn(asset2)).toBe('https://assset1.cdn.com/test.jpg'); | ||
expect(memoizedCdn(asset3)).toBe('https://assset2.cdn.com/test.jpg?size=sm'); | ||
expect(memoizedCdn(asset4)).toBe('https://assset0.cdn.com/test.png#sm'); | ||
}); | ||
expect(cdn(src, variants)).toBe('https://assset0.cdn.com/test.png'); | ||
expect(cdn(src, variants)).toBe('https://assset1.cdn.com/test.png'); | ||
expect(cdn(src, variants)).toBe('https://assset2.cdn.com/test.png'); | ||
expect(cdn(src, variants)).toBe('https://assset3.cdn.com/test.png'); | ||
expect(cdn(src, variants)).toBe('https://assset4.cdn.com/test.png'); | ||
expect(cdn(src, variants)).toBe('https://assset0.cdn.com/test.png'); | ||
expect(cdn(src, variants)).toBe('https://assset1.cdn.com/test.png'); | ||
test('returns same string for the same string', () => { | ||
expect(memoizedCdn(asset1)).toBe('https://assset0.cdn.com/test.png'); | ||
expect(memoizedCdn(asset2)).toBe('https://assset1.cdn.com/test.jpg'); | ||
expect(memoizedCdn(asset3)).toBe('https://assset2.cdn.com/test.jpg?size=sm'); | ||
expect(memoizedCdn(asset4)).toBe('https://assset0.cdn.com/test.png#sm'); | ||
}); |
15
cdn.js
var counter = require('./counter'); | ||
module.exports = function(src, variants) { | ||
module.exports = function(variants) { | ||
var variants = (variants || 4); | ||
var cache = {}; | ||
return src.replace(/%d/, counter(variants)); | ||
}; | ||
return function(src) { | ||
if (src in cache) { | ||
return cache[src]; | ||
} else { | ||
var save = src.replace(/%d/, counter(variants)); | ||
cache[src] = save; | ||
return save; | ||
} | ||
} | ||
} |
@@ -5,12 +5,4 @@ module.exports = (function() { | ||
return function(variants) { | ||
var response = count; | ||
count++ | ||
if (count > variants - 1) { | ||
count = 0; | ||
} | ||
return response; | ||
return count++ % variants; | ||
} | ||
})() |
{ | ||
"name": "@procore/cdn", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"description": "Resolve the procore CDN", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,4 +5,4 @@ # Resolve Procore CDN | ||
Browsers only allow 6 current requests to the same domain. In order to overcome this restriction procore has 5 | ||
subdomains for fastly. This helper function will alternate between the subdomains in a sequence of 0-4. | ||
Browsers only allow 6 current requests to the same domain. In order to overcome this restriction procore has 4 | ||
subdomains for fastly. This helper function will alternate between the subdomains in a sequence of 0-3 by default. The results are memoized, the same string will result with the same value. | ||
@@ -12,6 +12,16 @@ ## Usage | ||
```javascript | ||
import cdn from '@procore/cdn'; | ||
cdn(numberOfDomains: number) { | ||
return cached(bgImg: string) | ||
return string | ||
} | ||
``` | ||
## Example | ||
```javascript | ||
import _cdn from '@procore/cdn'; | ||
import testImg from './test.png'; | ||
import bgImg from './bg.jpg'; | ||
const cdn = _cdn(4) | ||
@@ -18,0 +28,0 @@ const App = () => { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
50
41
3395
1