Comparing version 1.0.2 to 2.0.0
{ | ||
"name": "agegate", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"description": "Limit access to your app with an age gate", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -58,10 +58,8 @@ # AgeGate | ||
expiry: Infinity | ||
}; | ||
} | ||
let gate = new AgeGate(options, (err) => { | ||
if (err) | ||
throw new Error('You shall not pass'); | ||
else | ||
console.log('Fly, you fools'); | ||
}); | ||
if (err) throw new Error('You shall not pass'); | ||
else console.log('Fly, you fools'); | ||
}) | ||
``` | ||
@@ -87,2 +85,3 @@ | ||
**expiry** | `number`, `Infinity`, `Date` | `0` | | Sets the expiration of the cookie in seconds. `0` is session-only. `Infinity` is forever. Supply a [Date object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) for any custom length of time | ||
**domain** | `String` | `null` | | Cookie path | ||
@@ -89,0 +88,0 @@ **`[1]`** Format each country's data in an `Object`, and set the country order in the enclosing `Array`. For example: |
134
src/index.js
@@ -1,15 +0,14 @@ | ||
import * as data from './data'; | ||
import cookies from './cookies'; | ||
import * as data from './data' | ||
const FORM_ELEMENTS = ['year', 'month', 'day', 'country', 'remember']; | ||
const FORM_ELEMENTS = ['year', 'month', 'day', 'country', 'remember'] | ||
export default class AgeGate { | ||
constructor(opts, cb) { | ||
this.options = opts; | ||
this.callback = cb; | ||
this.isEnabled.data && this.validateData(opts.data); | ||
this.options = opts | ||
this.callback = cb | ||
this.isEnabled.data && this.validateData(opts.data) | ||
// render | ||
this.isEnabled.countries && this.populate(); | ||
this.options.form.addEventListener('submit', this.submit.bind(this)); | ||
this.isEnabled.countries && this.populate() | ||
this.options.form.addEventListener('submit', this.submit.bind(this)) | ||
} | ||
@@ -25,11 +24,11 @@ | ||
data: !!this.options.data | ||
}; | ||
} | ||
} | ||
get legalAge() { | ||
return parseInt(this.options.age, 10) || 18; | ||
return parseInt(this.options.age, 10) || 18 | ||
} | ||
get data() { | ||
return this.options.data || data; | ||
return this.options.data || data | ||
} | ||
@@ -41,17 +40,17 @@ | ||
get ages() { | ||
let ages = {}; | ||
let ages = {} | ||
if (this.options.data) { | ||
ages = this.data.reduce((total, item) => { | ||
total[item.code] = item.age; | ||
return total; | ||
}, ages); | ||
total[item.code] = item.age | ||
return total | ||
}, ages) | ||
} | ||
else { | ||
for (let cont in this.data) { | ||
this.data[cont].map(country => ages[country.code] = country.age); | ||
this.data[cont].map(country => ages[country.code] = country.age) | ||
} | ||
} | ||
return ages; | ||
return ages | ||
} | ||
@@ -64,13 +63,10 @@ | ||
*/ | ||
validateData(data) { | ||
let random = Math.floor(Math.random() * (data.length - 0) + 0); | ||
validateData (data) { | ||
let random = Math.floor(Math.random() * (data.length - 0) + 0) | ||
// ensure: containing Array and Object keys | ||
let ok = (Array.isArray(data) || data instanceof Array); | ||
ok = ok && ['code', 'name', 'age'].every(k => data[random].hasOwnProperty(k)); | ||
let ok = (Array.isArray(data) || data instanceof Array) | ||
ok = ok && ['code', 'name', 'age'].every(k => data[random].hasOwnProperty(k)) | ||
if (ok) | ||
return data; | ||
else | ||
this.respond(false, 'Supplied data is invalid'); | ||
return ok ? data : this.respond(false, 'Supplied data is invalid') | ||
} | ||
@@ -82,35 +78,34 @@ | ||
populate() { | ||
let select = this.options.form.querySelector('select'); | ||
select.innerHTML = ''; // assume it's not empty | ||
let select = this.options.form.querySelector('select') | ||
select.innerHTML = '' // assume it's not empty | ||
// attempt to use user-supplied data | ||
if (this.isEnabled.data) | ||
this.data.forEach(country => select.appendChild( createOption(country) )); | ||
this.data.forEach(country => select.appendChild( createOption(country) )) | ||
// fallback to default data (continent-separated) | ||
else | ||
Object.keys(data).forEach(continent => { | ||
let group = document.createElement('optgroup'); | ||
group.label = continent; | ||
else Object.keys(data).forEach(continent => { | ||
let group = document.createElement('optgroup') | ||
group.label = continent | ||
// create the <option> for each country | ||
for (let i=0; i<data[continent].length; i++) { | ||
let country = data[continent][i]; | ||
group.appendChild( createOption(country) ); | ||
} | ||
// create the <option> for each country | ||
for (let i=0 i<data[continent].length i++) { | ||
let country = data[continent][i] | ||
group.appendChild( createOption(country) ) | ||
} | ||
select.appendChild(group); | ||
}); | ||
select.appendChild(group) | ||
}) | ||
// create the <option> element | ||
function createOption(country) { | ||
let option = document.createElement('option'); | ||
let option = document.createElement('option') | ||
for (let attr in country) { | ||
option.dataset[attr] = country[attr]; | ||
option.dataset[attr] = country[attr] | ||
} | ||
option.value = country.code; | ||
option.textContent = country.name; | ||
option.value = country.code | ||
option.textContent = country.name | ||
return option; | ||
return option | ||
} | ||
@@ -126,23 +121,23 @@ } | ||
submit(e) { | ||
e.preventDefault(); | ||
e.preventDefault() | ||
let elements = e.target.elements; | ||
let elements = e.target.elements | ||
// create an object from the form data | ||
this.formData = FORM_ELEMENTS.reduce((collection, key) => { | ||
if (!elements[key]) return collection; | ||
if (!elements[key]) return collection | ||
switch (key) { | ||
case 'remember': | ||
collection[key] = elements[key].checked; | ||
break; | ||
collection[key] = elements[key].checked | ||
break | ||
default: | ||
collection[key] = elements[key].value; | ||
break; | ||
collection[key] = elements[key].value | ||
break | ||
} | ||
return collection; | ||
}, {}); | ||
return collection | ||
}, {}) | ||
this.respond( this.verify(this.formData) ); | ||
this.respond( this.verify(this.formData) ) | ||
} | ||
@@ -158,4 +153,4 @@ | ||
*/ | ||
verify(formData) { | ||
let ok = false, legalAge = this.ages[formData.country] || this.legalAge; | ||
verify (formData) { | ||
let ok = false, legalAge = this.ages[formData.country] || this.legalAge | ||
let bday = [ | ||
@@ -165,13 +160,13 @@ parseInt(formData.year, 10), | ||
parseInt(formData.day, 10) || 1 | ||
].join('/'); | ||
let age = ~~((new Date().getTime() - +new Date(bday)) / (31557600000)); | ||
].join('/') | ||
let age = ~~((new Date().getTime() - +new Date(bday)) / (31557600000)) | ||
if (age >= legalAge) { | ||
let expiry = !!formData.remember? this.options.expiry : null; | ||
this.saveCookie(expiry); | ||
let expiry = !!formData.remember? this.options.expiry : null | ||
this.saveCookie(expiry) | ||
ok = true; | ||
ok = true | ||
} | ||
return ok; | ||
return ok | ||
} | ||
@@ -184,4 +179,7 @@ | ||
*/ | ||
saveCookie(expiry=null) { | ||
cookies.setItem('old_enough', true, expiry); | ||
saveCookie (expiry=null) { | ||
const path = this.options.path || null | ||
const domain = this.options.domain || null | ||
cookies.setItem('old_enough', true, expiry, path, domain) | ||
} | ||
@@ -195,8 +193,6 @@ | ||
*/ | ||
respond(success=false, message='Age verification failure') { | ||
if (success) | ||
this.callback(null); | ||
else | ||
this.callback(new Error(`[AgeGate] ${message}`)); | ||
respond (success=false, message='Age verification failure') { | ||
if (success) this.callback(null) | ||
else this.callback(new Error(`[AgeGate] ${message}`)) | ||
} | ||
} |
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
41925
741
105