1 |
|
2 | var util = module.exports
|
3 |
|
4 | util.extends = function (Child, Parent) {
|
5 |
|
6 | if(typeof Parent === "string") {
|
7 | Parent = require("./model/" + Parent)
|
8 | }
|
9 |
|
10 | Child.prototype = Object.create(Parent.prototype)
|
11 | Child.prototype.constructor = Child
|
12 | Child.prototype.__super__ = Parent.prototype
|
13 | }
|
14 |
|
15 | util.createQueryString = function (page, size, sort) {
|
16 | var obj = null
|
17 | if(size !== undefined && size !== null && size > 0) {
|
18 | obj = {}
|
19 | obj.size = size
|
20 | }
|
21 | if(page !== undefined && page !== null && page > 0) {
|
22 | obj = obj || {}
|
23 | obj.page = page
|
24 | }
|
25 | if(sort !== undefined && sort !== null && sort.length > 0) {
|
26 | obj = obj || {}
|
27 | obj.sort = sort
|
28 | }
|
29 | return obj != null ? util.buildQueryString(obj) : ""
|
30 | }
|
31 |
|
32 | util.buildQueryString = function (obj, prefix) {
|
33 | var qs = ""
|
34 | var qsparts = []
|
35 | for(var key in obj) {
|
36 | qsparts.push(key + "=" + obj[key])
|
37 | }
|
38 | qs = (prefix === undefined ? "?" : prefix) + qsparts.join("&")
|
39 | return qs
|
40 | }
|
41 |
|
42 | util.parseDate = function (timestamp, defaultValue) {
|
43 | if(typeof timestamp === "undefined") {
|
44 | return new Date()
|
45 | }
|
46 | if(timestamp instanceof Date) {
|
47 | return timestamp
|
48 | }
|
49 | if(typeof timestamp === "string") {
|
50 | return new Date(timestamp)
|
51 | }
|
52 | if(typeof timestamp === "number") {
|
53 |
|
54 | timestamp *= (timestamp.toString().length === 10) ? 1000 : 1
|
55 | return new Date(timestamp)
|
56 | }
|
57 |
|
58 |
|
59 | if(defaultValue !== undefined) {
|
60 | return defaultValue
|
61 | }
|
62 | throw new Error("Cannot parse date value: " + timestamp)
|
63 | }
|
64 |
|
65 | util.toUNIX = function (value) {
|
66 | value = value || new Date()
|
67 | var timestamp = util.parseDate(value)
|
68 | return Math.round(timestamp.getTime() / 1000)
|
69 | }
|