All files cookies.ts

100% Statements 13/13
100% Branches 2/2
100% Functions 3/3
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37                  2x 2x 2x 2x               4x 4x 4x 5x 5x   2x             1x      
export default {
    /**
     * 设置cookies
     * @param {string} cname 键值
     * @param {string} cvalue 数据
     * @param {number} second 过期时间(s)
     * @returns {void}
     */
    set: (cname: string, cvalue: string, second?: number): void => {
        const d = new Date()
        d.setTime(d.getTime() + second * 1000)
        const expires = 'expires=' + d.toUTCString()
        document.cookie = cname + '=' + cvalue + '; ' + expires
    },
    /**
     * 获取对应key的cookies
     * @param {string} cname 键值
     * @returns {string}
     */
    get: (cname: string): string => {
        const name = cname + '='
        const ca = document.cookie.split(';')
        for (let i = 0; i < ca.length; i++) {
            const c = ca[i].trim()
            if (c.indexOf(name) === 0) return c.substring(name.length, c.length)
        }
        return ''
    },
    /**
     * 移除对应cname的cookies
     * @param {string} cname 键值
     */
    remove: (cname: string) => {
        document.cookie = cname + '=; expires=Thu, 01 Jan 1970 00:00:00 GMT'
    },
}