amazon-helpers
Advanced tools
Comparing version 0.1.4 to 0.2.0
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
/*! amazon-helpers by Manuel Bieh | ||
@@ -9,60 +11,58 @@ * Tiny helper library to extract an ASIN/ISBN number from an Amazon URL or create a (optionally localized) Amazon URL out of an ASIN/ISBN. | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}); | ||
var helpers = { | ||
getIdent: function getIdent(urlOrAsin) { | ||
getIdent: function getIdent(urlOrAsin) { | ||
if (typeof urlOrAsin === 'string' && (urlOrAsin.length === 10 // ASIN | ||
|| urlOrAsin.length === 13) // ISBN | ||
&& !!urlOrAsin.match(/^([a-zA-Z0-9]*)$/) === true) { | ||
if (typeof urlOrAsin === 'string' && (urlOrAsin.length === 10 // ASIN | ||
|| urlOrAsin.length === 13 // ISBN | ||
) && !!urlOrAsin.match(/^([a-zA-Z0-9]*)$/) === true) { | ||
return urlOrAsin; | ||
} | ||
return urlOrAsin; | ||
} | ||
if (helpers.getIdentByUrl(urlOrAsin)) { | ||
if (helpers.getIdentByUrl(urlOrAsin)) { | ||
return helpers.getIdentByUrl(urlOrAsin).asin; | ||
} | ||
}, | ||
return helpers.getIdentByUrl(urlOrAsin).asin; | ||
} | ||
}, | ||
getProductUrl: function getProductUrl(urlOrAsin, tld) { | ||
getProductUrl: function getProductUrl(urlOrAsin, tld) { | ||
var ident = helpers.getIdentByUrl(urlOrAsin); | ||
var ident = helpers.getIdentByUrl(urlOrAsin); | ||
if (ident) { | ||
if (ident) { | ||
tld = tld || ident.tld || 'com'; | ||
return 'http://www.amazon.' + tld + '/dp/' + ident.asin; | ||
} | ||
tld = tld || ident.tld || 'com'; | ||
return 'http://www.amazon.' + tld + '/dp/' + ident.asin; | ||
} | ||
if (typeof urlOrAsin === 'string' && (urlOrAsin.length === 10 // ASIN | ||
|| urlOrAsin.length === 13) // ISBN | ||
&& !!urlOrAsin.match(/^([a-zA-Z0-9]*)$/) === true) { | ||
if (typeof urlOrAsin === 'string' && (urlOrAsin.length === 10 // ASIN | ||
|| urlOrAsin.length === 13 // ISBN | ||
) && !!urlOrAsin.match(/^([a-zA-Z0-9]*)$/) === true) { | ||
tld = tld || 'com'; | ||
return 'http://www.amazon.' + tld + '/dp/' + urlOrAsin; | ||
} | ||
}, | ||
tld = tld || 'com'; | ||
return 'http://www.amazon.' + tld + '/dp/' + urlOrAsin; | ||
} | ||
}, | ||
getIdentByUrl: function getIdentByUrl(url) { | ||
getSecureProductUrl: function getSecureProductUrl(urlOrAsin, tld) { | ||
return helpers.getProductUrl(urlOrAsin, tld).replace(/^http:/, 'https:'); | ||
}, | ||
var URLREGEX = /https?:\/\/(www\.)?(.*)amazon\.([a-z\.]{2,5})\/(.*)\/?(?:dp|o|gp|-)\/(aw\/d\/|product\/)?(B[0-9]{2}[0-9A-Z]{7}|[0-9]{9}(?:X|[0-9]))/; | ||
var ident = typeof url === 'string' && url.match(URLREGEX); | ||
getIdentByUrl: function getIdentByUrl(url) { | ||
if (ident) { | ||
var URLREGEX = /https?:\/\/(www\.)?(.*)amazon\.([a-z\.]{2,5})\/(.*)\/?(?:dp|o|gp|-)\/(aw\/d\/|product\/)?(B[0-9]{2}[0-9A-Z]{7}|[0-9]{9}(?:X|[0-9]))/; | ||
var ident = typeof url === 'string' && url.match(URLREGEX); | ||
return { | ||
asin: ident.splice(-1)[0], | ||
tld: ident[3] | ||
}; | ||
} | ||
if (ident) { | ||
return { | ||
asin: ident.splice(-1)[0], | ||
tld: ident[3] | ||
}; | ||
} | ||
} | ||
}; | ||
exports['default'] = helpers; | ||
module.exports = exports['default']; | ||
module.exports = helpers; |
{ | ||
"name": "amazon-helpers", | ||
"version": "0.1.4", | ||
"version": "0.2.0", | ||
"description": "Some tiny helper functions for Amazon URLs", | ||
@@ -5,0 +5,0 @@ "main": "dist/amazon-helpers.js", |
@@ -1,2 +0,2 @@ | ||
# Amazon Helpers | ||
# Amazon Helpers [![Build Status](https://travis-ci.org/manuelbieh/amazon-helpers.svg?branch=master)](https://travis-ci.org/manuelbieh/amazon-helpers) | ||
@@ -34,3 +34,3 @@ Tiny helper library to extract an ASIN/ISBN number from an Amazon URL or create a (optionally localized) Amazon URL out of an ASIN/ISBN. | ||
You can specify a TLD as second argument to convert your URL to another country | ||
`amazonHelpers.getProductUrl('B000MTST70', 'co.uk')` | ||
`amazonHelpers.getProductUrl('http://www.amazon.com/dp/B000MTST70', 'co.uk')` | ||
@@ -43,3 +43,3 @@ => `http://www.amazon.co.uk/dp/B000MTST70` | ||
### .getIdent(urlOrAsin) | ||
### .getIdentByUrl(urlOrAsin) | ||
@@ -46,0 +46,0 @@ `amazonHelpers.getIdentByUrl('http://amazon.com/gp/product/B00L3KNWBU')` |
@@ -57,2 +57,6 @@ /*! amazon-helpers by Manuel Bieh | ||
getSecureProductUrl: (urlOrAsin, tld) => { | ||
return helpers.getProductUrl(urlOrAsin, tld).replace(/^http:/,'https:'); | ||
}, | ||
getIdentByUrl: (url) => { | ||
@@ -76,2 +80,2 @@ | ||
export default helpers; | ||
module.exports = helpers; |
@@ -100,2 +100,55 @@ var amazonHelpers = require('../dist/amazon-helpers'); | ||
describe('#getSecureProductUrl()', function() { | ||
it('should return a https product URL', function () { | ||
assert.equal( | ||
amazonHelpers.getSecureProductUrl('http://www.amazon.de/Highland-Park-Single-Scotch-Whisky/dp/B002E2LO5M/'), | ||
'https://www.amazon.de/dp/B002E2LO5M' | ||
); | ||
assert.equal( | ||
amazonHelpers.getSecureProductUrl('http://www.amazon.com/gp/product/B000MTST70/'), | ||
'https://www.amazon.com/dp/B000MTST70' | ||
); | ||
assert.equal( | ||
amazonHelpers.getSecureProductUrl('http://www.amazon.fr/Saveur-degustation-Plaque-perfore-baguettes/dp/B004U95E80/'), | ||
'https://www.amazon.fr/dp/B004U95E80' | ||
); | ||
assert.equal( | ||
amazonHelpers.getSecureProductUrl('http://www.amazon.co.jp/Wrong-Pick-Girls-Dungeon-manga-ebook/dp/B00XMG4CYE/ref=sr_1_1?ie=UTF8&qid=1442276590&sr=8-1&keywords=manga'), | ||
'https://www.amazon.co.jp/dp/B00XMG4CYE' | ||
); | ||
assert.equal( | ||
amazonHelpers.getSecureProductUrl('B002E2LO5M'), | ||
'https://www.amazon.com/dp/B002E2LO5M' | ||
); | ||
assert.equal( | ||
amazonHelpers.getSecureProductUrl('B002E2LO5M', 'de'), | ||
'https://www.amazon.de/dp/B002E2LO5M' | ||
); | ||
}); | ||
it('should return a https product URL for a specific TLD', function () { | ||
assert.equal( | ||
amazonHelpers.getSecureProductUrl('http://www.amazon.com/AmazonBasics-High-Speed-HDMI-Cable-2-Pack/dp/B00L3KNWBU/ref=sr_1_3?ie=UTF8', 'co.uk'), | ||
'https://www.amazon.co.uk/dp/B00L3KNWBU' | ||
); | ||
assert.equal( | ||
amazonHelpers.getSecureProductUrl('http://amazon.com/dp/0596519796', 'de'), | ||
'https://www.amazon.de/dp/0596519796' | ||
); | ||
}); | ||
}); | ||
describe('#getIdentByUrl()', function() { | ||
@@ -102,0 +155,0 @@ |
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
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
11946
7
230