Library for working with cookies (tasty-cookies)

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

I will give an example on 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');

Once upon a time in the back streets of the network I found such a wonderful thing like jQuery cookie , but over time I began to realize that one method was no longer enough for me to conveniently work with cookies.

Same example with jQuery cookie:

// Добавляем строку
$.cookie('string', 'Моя строка');
// Добавляю объект
$.cookie('object', {a: 1, b: 2});
// Получаю объект
var object = $.cookie('object');

Not so 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 little “strange”, “tricky”. The methods putObject, getObject are generally terrible, 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 for one so warm, cozy that would perform the simplest things and provide excellent tools for working with cookies. I went deep into the search and, to my surprise, I didn’t find anything suitable for me, some libraries lacked methods, other methods are enough but they are strange to me. Maybe I'm too picky?

Based on all of this, I decided to invent my bike with the most round wheels and comfortable seating. I think this is right. The work dragged on for several days, in general, this is what the tasty-cookies library itself turned out to be, and the Russian documentation .

Tasty cookies work example:

  // Добавляем строку
  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, job evaluations, and of course suggestions for improving the library.

Also popular now: