Skip to content

Commit

Permalink
Fixed age and date functions, added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vitmalina committed Jul 26, 2014
1 parent a11ad7b commit cacf750
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 16 deletions.
56 changes: 48 additions & 8 deletions qa/tests/w2utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,46 @@
// **********************************
// -- Unit Tests: w2utils

test( "w2utils.date()", function () {
var dt = new Date();
var dt2 = (new Date()).getTime() - 86400000 * 5;
equal( w2utils.date(), '', "- no argument -" );
equal( w2utils.date(''), '', "- blank -" );
equal( w2utils.date(null), '', "- null -" );
equal( w2utils.date(undefined), '', "- undefined -" );
equal( w2utils.date({}), '', "- object -" );
equal( w2utils.date([]), '', "- array -" );
equal( w2utils.stripTags(w2utils.date(new Date())), w2utils.formatTime(dt), "Today" );
equal( w2utils.stripTags(w2utils.date((new Date()).getTime() - 86400000 )), w2utils.stripTags('Yesterday'), "Yesterday" );
equal( w2utils.stripTags(w2utils.date(dt2)), w2utils.formatDate(dt2, w2utils.settings.date_display), "5 days ago" );
});

test( "w2utils.age()", function () {
var dt = new Date();
equal( w2utils.age(), '', "- no argument -" );
equal( w2utils.age(''), '', "- blank -" );
equal( w2utils.age(null), '', "- null -" );
equal( w2utils.age(undefined), '', "- undefined -" );
equal( w2utils.age({}), '', "- object -" );
equal( w2utils.age([]), '', "- array -" );
equal( w2utils.stripTags(w2utils.age(new Date())), '0 sec', "Now" );
equal( w2utils.stripTags(w2utils.age((new Date()).getTime() - 4000 )), '4 secs', "4 secs" );
equal( w2utils.stripTags(w2utils.age((new Date()).getTime() + 4000 )), '0 sec', "future" );
equal( w2utils.stripTags(w2utils.age((new Date()).getTime() - 1000 * 60 * 5 )), '5 mins', "5 mins" );
equal( w2utils.stripTags(w2utils.age((new Date()).getTime() - 1000 * 60 * 45 )), '45 mins', "45 mins" );
equal( w2utils.stripTags(w2utils.age((new Date()).getTime() - 1000 * 60 * 60 * 2 )), '2 hours', "2 hours" );
equal( w2utils.stripTags(w2utils.age((new Date()).getTime() - 86400000)), '1 day', "Yesterday" );
equal( w2utils.stripTags(w2utils.age((new Date()).getTime() - 86400000 * 1.5)), '1 day', "Yesterday" );
equal( w2utils.stripTags(w2utils.age((new Date()).getTime() - 86400000 * 5)), '5 days', "5 days" );
equal( w2utils.stripTags(w2utils.age((new Date()).getTime() - 86400000 * 30)), '1 month', "1 month" );
equal( w2utils.stripTags(w2utils.age((new Date()).getTime() - 86400000 * 33)), '1.1 months', "over a month" );
equal( w2utils.stripTags(w2utils.age((new Date()).getTime() - 86400000 * 50)), '1.6 months', "over a month" );
equal( w2utils.stripTags(w2utils.age((new Date()).getTime() - 86400000 * 145)), '4.8 months', "over 4 months" );
equal( w2utils.stripTags(w2utils.age((new Date()).getTime() - 86400000 * 365)), '1 year', "one year" );
equal( w2utils.stripTags(w2utils.age((new Date()).getTime() - 86400000 * 420)), '1.1 years', "over one year" );
equal( w2utils.stripTags(w2utils.age((new Date()).getTime() - 86400000 * 365 * 20)), '19.9 years', "arround 20 years" );
});

test( "w2utils.formatNumber()", function () {
var values = {
'1,000' : '1000',
Expand Down Expand Up @@ -106,7 +146,7 @@ test( "w2utils.formatDateTime()", function () {
}
});

test( "w2utils.size()", function() {
test( "w2utils.formatSize()", function() {
var values = {
'' : 0,
'1 Bt' : 1,
Expand All @@ -120,15 +160,15 @@ test( "w2utils.size()", function() {
'2.5 GB' : 1024*1024*1024*2.5,
'2.9 TB' : 1024*1024*1024*1024*2.99
}
equal( w2utils.size(), '', "- no argument -" );
equal( w2utils.size(''), '', "- blank -" );
equal( w2utils.size(null), '', "- null -" );
equal( w2utils.size(undefined), '', "- undefined -" );
equal( w2utils.size({}), '', "- object -" );
equal( w2utils.size([]), '', "- array -" );
equal( w2utils.formatSize(), '', "- no argument -" );
equal( w2utils.formatSize(''), '', "- blank -" );
equal( w2utils.formatSize(null), '', "- null -" );
equal( w2utils.formatSize(undefined), '', "- undefined -" );
equal( w2utils.formatSize({}), '', "- object -" );
equal( w2utils.formatSize([]), '', "- array -" );

for (var v in values) {
equal( w2utils.size(values[v]), v, 'Test: ' + values[v] + ' = ' + v);
equal( w2utils.formatSize(values[v]), v, 'Test: ' + values[v] + ' = ' + v);
}
});

Expand Down
21 changes: 13 additions & 8 deletions src/w2utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var w2obj = w2obj || {}; // expose object to be able to overwrite default functi
*
* == 1.5 changes
* - added decimalSymbol
* - renamed size() -> formatSize()
*
************************************************/

Expand Down Expand Up @@ -67,7 +68,7 @@ var w2utils = (function () {
isTime : isTime,
age : age,
date : date,
size : size,
formatSize : formatSize,
formatNumber : formatNumber,
formatDate : formatDate,
formatTime : formatTime,
Expand Down Expand Up @@ -229,7 +230,7 @@ var w2utils = (function () {
}

function age (dateStr) {
if (dateStr === '' || dateStr == null) return '';
if (dateStr === '' || dateStr == null || (typeof dateStr == 'object' && !dateStr.getMonth)) return '';
var d1 = new Date(dateStr);
if (w2utils.isInt(dateStr)) d1 = new Date(Number(dateStr)); // for unix timestamps
if (d1 === 'Invalid Date') return '';
Expand All @@ -239,7 +240,7 @@ var w2utils = (function () {
var amount = '';
var type = '';
if (sec < 0) {
amount = '<span style="color: #aaa">future</span>';
amount = '<span style="color: #aaa">0 sec</span>';
type = '';
} else if (sec < 60) {
amount = Math.floor(sec);
Expand All @@ -254,18 +255,22 @@ var w2utils = (function () {
} else if (sec < 30*24*60*60) {
amount = Math.floor(sec/24/60/60);
type = 'day';
} else if (sec < 365.25*24*60*60) {
amount = Math.floor(sec/365.25/24/60/60*10)/10;
} else if (sec < 365*24*60*60) {
amount = Math.floor(sec/30/24/60/60*10)/10;
type = 'month';
} else if (sec >= 365.25*24*60*60) {
} else if (sec < 365*4*24*60*60) {
amount = Math.floor(sec/365/24/60/60*10)/10;
type = 'year';
} else if (sec >= 365*4*24*60*60) {
// factor in leap year shift (only older then 4 years)
amount = Math.floor(sec/365.25/24/60/60*10)/10;
type = 'year';
}
return amount + ' ' + type + (amount > 1 ? 's' : '');
}

function date (dateStr) {
if (dateStr === '' || dateStr == null) return '';
if (dateStr === '' || dateStr == null || (typeof dateStr == 'object' && !dateStr.getMonth)) return '';
var d1 = new Date(dateStr);
if (w2utils.isInt(dateStr)) d1 = new Date(Number(dateStr)); // for unix timestamps
if (d1 === 'Invalid Date') return '';
Expand All @@ -288,7 +293,7 @@ var w2utils = (function () {
return '<span title="'+ dd1 +' ' + time2 +'">'+ dsp +'</span>';
}

function size (sizeStr) {
function formatSize (sizeStr) {
if (!w2utils.isFloat(sizeStr) || sizeStr === '') return '';
sizeStr = parseFloat(sizeStr);
if (sizeStr === 0) return 0;
Expand Down
21 changes: 21 additions & 0 deletions tasks.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Tasks:

при выделении рэнжа нужно событие когда рэнж выделился

---

Building Data Intensive UI in JavaScript with W2UI

UI is one of the most important aspects of your web application, as often your entire applications will be judged by how
it looks and feels to the end user. There are a number of options when it comes to building a desktop-like web application,
but there is a big dilemma. All of the options are either proprietary solutions like ExtJS and KendoUI or mismatched sets of
open-source libraries that do not always play nicely together.

W2UI intends to fill this gap by providing a complete, minimal set of UI widgets to build your next data intensive web app
and enjoy doing it. In this talk I will overview available options, talk about pros and cons of various UI solution for data
intensive web apps and demo how to use W2UI to build a compelling web application that looks and feels as it was a native
desktop application.

http://w2ui.com/web/blog
http://davidwalsh.name/html5-input-types-alternative

0 comments on commit cacf750

Please sign in to comment.