📜 ⬆️ ⬇️

Library for working with cookies (tasty-cookies)

The story is old, I think so, everyone remembers window.cookie = '...' (or maybe someone uses it), a terribly inconvenient thing.

I will give an example on the native js:

//   function setCookie(key, value) { window.cookie = key + '=' + encodeURIComponent(JSON.stringify(value)); } //   function getCookie(key) { var matches = document.cookie.match(new RegExp( '(?:^|; )' + key.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + '=([^;]*)' )); return JSON.parse(decodeURIComponent(matches[1])); } //   setCookie('string', ' '); //   setCookie('object', {a: 1, b: 2}); //   var object = getCookie('object'); 


A long time ago, I found such a wonderful thing in the back streets of the network as a jQuery cookie , but over time I began to realize that I already lacked one method for convenient work with cookies.
')
The same jQuery cookie example:

 //   $.cookie('string', ' '); //   $.cookie('object', {a: 1, b: 2}); //   var object = $.cookie('object'); 

Not long ago, I began to get acquainted with angular, and it is not strange that they also have their own implementation of cookies , not much better, but it seems to me a bit “strange”, “tricky”. The methods putObject, getObject are generally terrifying, and why are they?

And again an example:

 angular.module('cookiesExample', ['ngCookies']) .controller('ExampleController', ['$cookies', function($cookies) { //   $cookies.put('string', ' '); //   $cookies.putObject('object', {a: 1, b: 2}); //   var object = $cookies.getObject('object'); }]); 

I'm tired of this variety of colors, I would like something one so warm, cozy that would perform the most simple things and give excellent tools for working with cookies. I went deep into the search and to my surprise I didn’t find anything suitable for me, in some libraries there were not enough methods, in other methods it was enough but they were strange for me. Maybe I'm too picky?

On the basis of all this, I decided to invent my bicycle with the most round wheels and a comfortable seat. It seems to me right. The work dragged on for several days, in general, this is what the tasty-cookies library itself turned out well and Russian documentation .

Example of tasty cookies:

 Cookie.set({ //   string: ' ', //   object: {a: 1, b: 2} }); //   var object = Cookie.get('object'); 

It uses a JSON object, so if you need support for older browsers, you can pull up a rake like JSON 3 , though, what am I talking about?

I would like to hear critics, assessments of work, and of course, suggestions for improving the library.

Source: https://habr.com/ru/post/280998/


All Articles