源码学习目录

1. 前言

1.1 环境

  1. 操作系统: macOS 11.5.2
  2. 浏览器: Chrome 94.0.4606.81
  3. js-cookie 6.0.0

    1.2 阅读该文章可以get以下知识点

  • js-cookie源码

    2. 开始

    2.1 如何使用

    ```javascript import Cookies from ‘js-cookie’

Cookies.set(‘foo’, ‘bar’)

  1. <a name="JMB54"></a>
  2. ## 2.2 源码
  3. ```javascript
  4. // 初始化函数
  5. function init (converter, defaultAttributes) {
  6. // 设置cookie
  7. function set (name, value, attributes) {
  8. if (typeof document === 'undefined') {
  9. return
  10. }
  11. // 合并属性
  12. attributes = assign({}, defaultAttributes, attributes)
  13. // 设置过期时间
  14. if (typeof attributes.expires === 'number') {
  15. // 用了科学计数864e5=86400000,e5就是后面5个0,Date.now()是毫秒级别,864e5这里代表一天,
  16. attributes.expires = new Date(Date.now() + attributes.expires * 864e5)
  17. }
  18. if (attributes.expires) {
  19. attributes.expires = attributes.expires.toUTCString()
  20. }
  21. // 做了uri的编码
  22. name = encodeURIComponent(name)
  23. .replace(/%(2[346B]|5E|60|7C)/g, decodeURIComponent)
  24. .replace(/[()]/g, escape)
  25. var stringifiedAttributes = ''
  26. for (var attributeName in attributes) {
  27. if (!attributes[attributeName]) {
  28. continue
  29. }
  30. // 将参数通过; 累加
  31. stringifiedAttributes += '; ' + attributeName
  32. if (attributes[attributeName] === true) {
  33. continue
  34. }
  35. // Considers RFC 6265 section 5.2:
  36. // ...
  37. // 3. If the remaining unparsed-attributes contains a %x3B (";")
  38. // character:
  39. // Consume the characters of the unparsed-attributes up to,
  40. // not including, the first %x3B (";") character.
  41. // ...
  42. stringifiedAttributes += '=' + attributes[attributeName].split(';')[0]
  43. }
  44. return (document.cookie =
  45. name + '=' + converter.write(value, name) + stringifiedAttributes)
  46. }
  47. // 获取名称
  48. function get (name) {
  49. if (typeof document === 'undefined' || (arguments.length && !name)) {
  50. return
  51. }
  52. // To prevent the for loop in the first place assign an empty array
  53. // in case there are no cookies at all.
  54. // 将cookie分割成数组
  55. var cookies = document.cookie ? document.cookie.split('; ') : []
  56. var jar = {}
  57. for (var i = 0; i < cookies.length; i++) {
  58. var parts = cookies[i].split('=')
  59. var value = parts.slice(1).join('=')
  60. try {
  61. var found = decodeURIComponent(parts[0])
  62. jar[found] = converter.read(value, found)
  63. if (name === found) {
  64. break
  65. }
  66. } catch (e) {}
  67. }
  68. // 获取需要的数组
  69. return name ? jar[name] : jar
  70. }
  71. // 初始化返回一个对象,set,get,remove,只读attributes和converter
  72. return Object.create(
  73. {
  74. set: set,
  75. get: get,
  76. // remove 清空数据,如果有属性,以属性作为初始化值
  77. remove: function (name, attributes) {
  78. set(
  79. name,
  80. '',
  81. assign({}, attributes, {
  82. expires: -1
  83. })
  84. )
  85. },
  86. // 修改attributes,返回一个新的对象
  87. withAttributes: function (attributes) {
  88. return init(this.converter, assign({}, this.attributes, attributes))
  89. },
  90. // 修改converter,返回一个新的对象
  91. withConverter: function (converter) {
  92. return init(assign({}, this.converter, converter), this.attributes)
  93. }
  94. },
  95. {
  96. attributes: { value: Object.freeze(defaultAttributes) },
  97. converter: { value: Object.freeze(converter) }
  98. }
  99. )
  100. }
  101. export default init(defaultConverter, { path: '/' })
  102. /* eslint-enable no-var */

3. 总结

  1. js-cookie没什么特殊的,主要就是拼接字符串,分割字符串,设置一下过期时间
  2. 学会了科学计数法e{n},例如 1e4 = 10000 ,e后面的n表示有多少个n

    4. 参考文档

  3. https://github.com/js-cookie/js-cookie