Skip to content

Commit

Permalink
Cookies, Data
Browse files Browse the repository at this point in the history
Cookies
 - Added support for session cookies

Data
 - Added test code snippet to CreateGuid function
  • Loading branch information
Jemt committed Mar 3, 2016
1 parent 3d3e9a6 commit 1b4e1c8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
19 changes: 14 additions & 5 deletions Core/Cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,32 @@ Fit.Cookies = {};
/// <description> Create or update cookie - returns True on success, otherwise False </description>
/// <param name="name" type="string"> Unique cookie name </param>
/// <param name="value" type="string"> Cookie value (cannot contain semicolon!) </param>
/// <param name="seconds" type="integer"> Expiration time in seconds </param>
/// <param name="seconds" type="integer" default="undefined">
/// Optional expiration time in seconds. Creating a cookie with
/// no expiration time will cause it to expire when session ends.
/// </param>
/// </function>
Fit.Cookies.Set = function(name, value, seconds)
{
Fit.Validation.ExpectStringValue(name);
Fit.Validation.ExpectString(name);
Fit.Validation.ExpectInteger(seconds);
Fit.Validation.ExpectInteger(seconds, true);

if (value.indexOf(';') > -1)
{
throw new Error("Unable to set cookie - value contains illegal character: ';'");
return false;
}

var date = new Date();
date.setTime(date.getTime() + (seconds * 1000));
document.cookie = name + "=" + value + "; expires=" + date.toGMTString() + "; path=/";
var date = null;

if (Fit.Validation.IsSet(seconds) === true)
{
date = new Date();
date.setTime(date.getTime() + (seconds * 1000));
}

document.cookie = name + "=" + value + ((date !== null) ? "; expires=" + date.toGMTString() : "") + "; path=/";

return true;
}
Expand Down
30 changes: 30 additions & 0 deletions Core/Data.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,36 @@ Fit.Data.CreateGuid = function(dashFormat)
{
Fit.Validation.ExpectBoolean(dashFormat, true);

/*
// Test case proving that unique GUIDs are generated every time.
// Use a powerful computer to run this test, and a fast browser (e.g. Safari).
var guids = {};
var minors = 0;
for (var i = 0 ; i < 25000000 ; i++) // 25 million
{
var g = Fit.Data.CreateGuid();
minors++;
if (minors === 5000)
{
console.log("5000 more GUIDs generated");
minors = 0;
}
if (guids[g])
{
alert("GUID already in use: " + g);
break;
}
else
{
guids[g] = 1;
}
}
*/

var chars = "0123456789abcdef".split("");

var uuid = new Array();
Expand Down

0 comments on commit 1b4e1c8

Please sign in to comment.