forked from github/jsotp
add: Util.timecode static function
This commit is contained in:
+23
@@ -92,4 +92,27 @@ export class Util {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* format the time string to int
|
||||
*
|
||||
* @param {time}
|
||||
* @type {Date}
|
||||
* @desc the time need to be format
|
||||
*
|
||||
* @param {interval}
|
||||
* @type {Int}
|
||||
* @desc interval means the one-time password's life,
|
||||
* default to be 30.
|
||||
*
|
||||
* @return {Int}
|
||||
*/
|
||||
static timecode(time, interval) {
|
||||
let time_str = Date.parse(time).toString();
|
||||
|
||||
// fotmat the time, the ms is not needed.
|
||||
let format_time = time_str.substring(0, time_str.length-3);
|
||||
|
||||
return parseInt(parseInt(format_time) / interval);
|
||||
}
|
||||
|
||||
}
|
||||
+8
-1
@@ -31,6 +31,13 @@ describe('Util module test', function() {
|
||||
let input = 49957590;
|
||||
assert.equal("\u0000\u0000\u0000\u0000\u0002úJÖ", a.int_to_bytestring(input));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('static timecode() function', function() {
|
||||
it("should format the time string to int", function() {
|
||||
let now = new Date();
|
||||
assert.equal("number", typeof(a.timecode(now, 30)));
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user