-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
using closures to give the effect of private class fields
- Loading branch information
Showing
15 changed files
with
1,539 additions
and
1,563 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,121 +1,122 @@ | ||
let fepperUiInst; | ||
// TODO: Replace closure with private class field when there is greater browser support. | ||
export default function (cookieName, fepperUiInst) { | ||
class DataSaver { | ||
|
||
/** | ||
* Data Saver. Namespaced cookie-handling methods. | ||
* | ||
* @param {string} cookieName - The name of the cookie to store the data in. | ||
* @param {object} fepperUi - The Fepper UI instance. | ||
*/ | ||
constructor(cookieName, fepperUi) { // eslint-disable-line no-unused-vars | ||
this.cookieName = cookieName; | ||
} | ||
|
||
export default class { | ||
// In case fepperUi.cookies is undefined at instantiation. | ||
get cookies() { | ||
return fepperUiInst.cookies; | ||
} | ||
|
||
/** | ||
* Data Saver. Namespaced cookie-handling methods. | ||
* | ||
* @param {string} cookieName - The name of the cookie to store the data in. | ||
* @param {object} fepperUi - The Fepper UI instance. | ||
*/ | ||
constructor(cookieName, fepperUi) { | ||
fepperUiInst = fepperUi; | ||
// Methods. | ||
|
||
this.cookieName = cookieName; | ||
} | ||
/** | ||
* Add a given value to the cookie. Do not update if it already exists. | ||
* | ||
* @param {string} name - The name of the key. | ||
* @param {string} val - The value. | ||
*/ | ||
addValue(name, val) { | ||
const cookieValOrig = this.cookies.get(this.cookieName); | ||
let cookieValNew = name + '~' + val; | ||
|
||
// In case fepperUi.cookies is undefined at instantiation. | ||
get cookies() { | ||
return fepperUiInst.cookies; | ||
} | ||
if (cookieValOrig) { | ||
cookieValNew = cookieValOrig + '|' + cookieValNew; | ||
} | ||
|
||
// Methods. | ||
this.cookies.set(this.cookieName, cookieValNew); | ||
} | ||
|
||
/** | ||
* Add a given value to the cookie. Do not update if it already exists. | ||
* | ||
* @param {string} name - The name of the key. | ||
* @param {string} val - The value. | ||
*/ | ||
addValue(name, val) { | ||
const cookieValOrig = this.cookies.get(this.cookieName); | ||
let cookieValNew = name + '~' + val; | ||
/** | ||
* Update a value found in the cookie. If the key doesn't exist, add the value. | ||
* | ||
* @param {string} name - The name of the key. | ||
* @param {string} val - The value. | ||
*/ | ||
updateValue(name, val) { | ||
if (this.findValue(name)) { | ||
const cookieVal = this.cookies.get(this.cookieName) || ''; | ||
const cookieVals = cookieVal.split('|'); | ||
let cookieValNew = ''; | ||
|
||
for (let i = 0; i < cookieVals.length; i++) { | ||
const fieldVals = cookieVals[i].split('~'); | ||
|
||
if (fieldVals[0] === name) { | ||
fieldVals[1] = val; | ||
} | ||
|
||
if (i) { | ||
cookieValNew += '|'; | ||
} | ||
|
||
cookieValNew += fieldVals[0] + '~' + fieldVals[1]; | ||
} | ||
|
||
if (cookieValOrig) { | ||
cookieValNew = cookieValOrig + '|' + cookieValNew; | ||
this.cookies.set(this.cookieName, cookieValNew); | ||
} | ||
else { | ||
this.addValue(name, val); | ||
} | ||
} | ||
|
||
this.cookies.set(this.cookieName, cookieValNew); | ||
} | ||
|
||
/** | ||
* Update a value found in the cookie. If the key doesn't exist, add the value. | ||
* | ||
* @param {string} name - The name of the key. | ||
* @param {string} val - The value. | ||
*/ | ||
updateValue(name, val) { | ||
if (this.findValue(name)) { | ||
/** | ||
* Remove the given key. | ||
* | ||
* @param {string} name - The name of the key. | ||
*/ | ||
removeValue(name) { | ||
const cookieVal = this.cookies.get(this.cookieName) || ''; | ||
const cookieVals = cookieVal.split('|'); | ||
let k = 0; | ||
let cookieValNew = ''; | ||
|
||
for (let i = 0; i < cookieVals.length; i++) { | ||
const fieldVals = cookieVals[i].split('~'); | ||
|
||
if (fieldVals[0] === name) { | ||
fieldVals[1] = val; | ||
} | ||
if (fieldVals[0] !== name) { | ||
if (k) { | ||
cookieValNew += '|'; | ||
} | ||
|
||
if (i) { | ||
cookieValNew += '|'; | ||
cookieValNew += fieldVals[0] + '~' + fieldVals[1]; | ||
k++; | ||
} | ||
|
||
cookieValNew += fieldVals[0] + '~' + fieldVals[1]; | ||
} | ||
|
||
this.cookies.set(this.cookieName, cookieValNew); | ||
} | ||
else { | ||
this.addValue(name, val); | ||
} | ||
} | ||
|
||
/** | ||
* Remove the given key. | ||
* | ||
* @param {string} name - The name of the key. | ||
*/ | ||
removeValue(name) { | ||
const cookieVal = this.cookies.get(this.cookieName) || ''; | ||
const cookieVals = cookieVal.split('|'); | ||
let k = 0; | ||
let cookieValNew = ''; | ||
|
||
for (let i = 0; i < cookieVals.length; i++) { | ||
const fieldVals = cookieVals[i].split('~'); | ||
|
||
if (fieldVals[0] !== name) { | ||
if (k) { | ||
cookieValNew += '|'; | ||
} | ||
|
||
cookieValNew += fieldVals[0] + '~' + fieldVals[1]; | ||
k++; | ||
} | ||
} | ||
/** | ||
* Find the value using the given key. | ||
* | ||
* @param {string} name - The name of the key. | ||
* @returns {string} The value of the key or empty string if the value isn't found. | ||
*/ | ||
findValue(name) { | ||
const cookieVal = this.cookies.get(this.cookieName) || ''; | ||
const cookieVals = cookieVal.split('|'); | ||
|
||
this.cookies.set(this.cookieName, cookieValNew); | ||
} | ||
for (let i = 0; i < cookieVals.length; i++) { | ||
const fieldVals = cookieVals[i].split('~'); | ||
|
||
/** | ||
* Find the value using the given key. | ||
* | ||
* @param {string} name - The name of the key. | ||
* @returns {string} The value of the key or empty string if the value isn't found. | ||
*/ | ||
findValue(name) { | ||
const cookieVal = this.cookies.get(this.cookieName) || ''; | ||
const cookieVals = cookieVal.split('|'); | ||
|
||
for (let i = 0; i < cookieVals.length; i++) { | ||
const fieldVals = cookieVals[i].split('~'); | ||
|
||
if (fieldVals[0] === name) { | ||
return fieldVals[1]; | ||
if (fieldVals[0] === name) { | ||
return fieldVals[1]; | ||
} | ||
} | ||
} | ||
|
||
return ''; | ||
return ''; | ||
} | ||
} | ||
|
||
return new DataSaver(cookieName, fepperUiInst); | ||
} |
Oops, something went wrong.