Get and set on every property (for cookies)

47 Views Asked by At

I want to do cookie.c.something = "hello". That will set the document.cookie to "something=hello;". Then I can get it back again with cookie.c.something. I have already made the get function, but I don't know how to do the set function. (when doing cookie.c.something = "hello" it calls the getter)

I thought this might work, then I realized that I can't get the property in the set function:

class cookie {
    static get c() {
        return this.object
    }

    /* What I want: */
    static set c(setter) {
        this.object["uses property 'hello'"] = setter
    }

    static get raw() {
        return document.cookie;
    }
    static get object(){
        let rem = {}
        const cookies = cookie.raw.split(';').map(cookie => cookie.trim())
        for (const cookieString of cookies) {
            const [cookieName, cookieValue] = cookieString.split('=')
            rem[`${decodeURIComponent(cookieName)}`] = decodeURIComponent(cookieValue)
        }
        return rem
    }
}

I later found:

let myObj = {
    id: 1,
    name: 'tomato',
};

for(let fieldName of Object.keys(myObj)) {
    console.log(`fieldName = ${fieldName}`);
    Object.defineProperty(myObj, fieldName + "_property", {
        set: function(value) {
            this[fieldName] = value;
        },
        get: function() {
            return this[fieldName];
        }
    });
}

myObj.id_property = 55; // setting id_property will set id
console.log(myObj.id); // so this will write 55

myObj.id = 123; // setting id
console.log(myObj.id_property); // this will write 123 because id_property returns id field

This almost would work, but it only will work for the once that I already have defined. I want it to work on all.

1

There are 1 best solutions below

1
stio On

Working code:

class cookie {
    static get c() {
        return new Proxy(this.object, {
            set: function(target, property, value) {
                cookie.raw = `${encodeURIComponent(property)}=${encodeURIComponent(value)}`;
                target[property] = value;
                return true;
            }
        });
    }
    static get(name) {
        const cookies = cookie.raw.split(';').map(cookie => cookie.trim())
        for (const cookieString of cookies) {
            const [cookieName, cookieValue] = cookieString.split('=')
            if (cookieName === name) {
                return decodeURIComponent(cookieValue)
            }
        }
        return undefined
    }
    static set(name, value, { expires = false, domain = info.mainDomain, path = false, secure = false } = {}) {
        // console.log(name, value, expires, domain, path, secure)
        let cookieString = `${encodeURIComponent(name)}=${encodeURIComponent(value)}`
        if (expires instanceof Date) {
            cookieString += `; expires=${expires.toUTCString()}`
        }
        if (domain) {
            cookieString += `; domain=${domain}`
        }
        if (path) {
            cookieString += `; path=${path}`
        }
        if (secure) {
            cookieString += `; secure`
        }
        cookie.raw = cookieString
    }
    static delete(name, {domain = info.mainDomain, path = false, secure = false } = {}){
        this.set(name, "", {expires: new Date("Thu, 01 Jan 1970 00:00:01 GMT"), path: path, domain: domain, secure: secure})
    }
    static deleteAll({domain = info.mainDomain, path = false, secure = false } = {}){
        this.array.forEach((name)=>{
            console.log(name.cookieName)
            this.delete(name.cookieName, {expires: new Date("Thu, 01 Jan 1970 00:00:01 GMT"), path: path, domain: domain, secure: secure})
        })
    }
    static get raw() {
        return document.cookie;
    }
    static set raw(setter) {
        document.cookie = setter
    }
    static get array(){
        let rem = []
        const cookies = cookie.raw.split(';').map(cookie => cookie.trim())
        for (const cookieString of cookies) {
            const [cookieName, cookieValue] = cookieString.split('=')
            rem.push(
                {
                    cookieName: decodeURIComponent(cookieName),
                    cookieValue: decodeURIComponent(cookieValue)
                }
            )
        }
        return rem
    }
    static get object(){
        let rem = {}
        const cookies = cookie.raw.split(';').map(cookie => cookie.trim())
        for (const cookieString of cookies) {
            const [cookieName, cookieValue] = cookieString.split('=')
            rem[`${decodeURIComponent(cookieName)}`] = decodeURIComponent(cookieValue)
        }
        return rem
    }
}