Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Video-Downloader/api/src/processing/cookie/cookie.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
48 lines (37 sloc)
1.04 KB
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
import { strict as assert } from 'node:assert'; | |
export default class Cookie { | |
constructor(input) { | |
assert(typeof input === 'object'); | |
this._values = {}; | |
for (const [ k, v ] of Object.entries(input)) | |
this.set(k, v); | |
} | |
set(key, value) { | |
const old = this._values[key]; | |
if (old === value) | |
return false; | |
this._values[key] = value; | |
return true; | |
} | |
unset(keys) { | |
for (const key of keys) delete this._values[key] | |
} | |
static fromString(str) { | |
const obj = {}; | |
str.split('; ').forEach(cookie => { | |
const key = cookie.split('=')[0]; | |
const value = cookie.split('=').splice(1).join('='); | |
obj[key] = value | |
}) | |
return new Cookie(obj) | |
} | |
toString() { | |
return Object.entries(this._values).map(([ name, value ]) => `${name}=${value}`).join('; ') | |
} | |
toJSON() { | |
return this.toString() | |
} | |
values() { | |
return Object.freeze({ ...this._values }) | |
} | |
} |