Updated from some -dev modules to alpha, beta or full releases
[yaffs-website] / node_modules / uncss / node_modules / qs / lib / utils.js
1 'use strict';
2
3 var hexTable = (function () {
4     var array = new Array(256);
5     for (var i = 0; i < 256; ++i) {
6         array[i] = '%' + ((i < 16 ? '0' : '') + i.toString(16)).toUpperCase();
7     }
8
9     return array;
10 }());
11
12 var has = Object.prototype.hasOwnProperty;
13
14 exports.arrayToObject = function (source, options) {
15     var obj = options.plainObjects ? Object.create(null) : {};
16     for (var i = 0; i < source.length; ++i) {
17         if (typeof source[i] !== 'undefined') {
18             obj[i] = source[i];
19         }
20     }
21
22     return obj;
23 };
24
25 exports.merge = function (target, source, options) {
26     if (!source) {
27         return target;
28     }
29
30     if (typeof source !== 'object') {
31         if (Array.isArray(target)) {
32             target.push(source);
33         } else if (typeof target === 'object') {
34             if (options.plainObjects || options.allowPrototypes || !has.call(Object.prototype, source)) {
35                 target[source] = true;
36             }
37         } else {
38             return [target, source];
39         }
40
41         return target;
42     }
43
44     if (typeof target !== 'object') {
45         return [target].concat(source);
46     }
47
48     var mergeTarget = target;
49     if (Array.isArray(target) && !Array.isArray(source)) {
50         mergeTarget = exports.arrayToObject(target, options);
51     }
52
53         return Object.keys(source).reduce(function (acc, key) {
54         var value = source[key];
55
56         if (has.call(acc, key)) {
57             acc[key] = exports.merge(acc[key], value, options);
58         } else {
59             acc[key] = value;
60         }
61                 return acc;
62     }, mergeTarget);
63 };
64
65 exports.decode = function (str) {
66     try {
67         return decodeURIComponent(str.replace(/\+/g, ' '));
68     } catch (e) {
69         return str;
70     }
71 };
72
73 exports.encode = function (str) {
74     // This code was originally written by Brian White (mscdex) for the io.js core querystring library.
75     // It has been adapted here for stricter adherence to RFC 3986
76     if (str.length === 0) {
77         return str;
78     }
79
80     var string = typeof str === 'string' ? str : String(str);
81
82     var out = '';
83     for (var i = 0; i < string.length; ++i) {
84         var c = string.charCodeAt(i);
85
86         if (
87             c === 0x2D || // -
88             c === 0x2E || // .
89             c === 0x5F || // _
90             c === 0x7E || // ~
91             (c >= 0x30 && c <= 0x39) || // 0-9
92             (c >= 0x41 && c <= 0x5A) || // a-z
93             (c >= 0x61 && c <= 0x7A) // A-Z
94         ) {
95             out += string.charAt(i);
96             continue;
97         }
98
99         if (c < 0x80) {
100             out = out + hexTable[c];
101             continue;
102         }
103
104         if (c < 0x800) {
105             out = out + (hexTable[0xC0 | (c >> 6)] + hexTable[0x80 | (c & 0x3F)]);
106             continue;
107         }
108
109         if (c < 0xD800 || c >= 0xE000) {
110             out = out + (hexTable[0xE0 | (c >> 12)] + hexTable[0x80 | ((c >> 6) & 0x3F)] + hexTable[0x80 | (c & 0x3F)]);
111             continue;
112         }
113
114         i += 1;
115         c = 0x10000 + (((c & 0x3FF) << 10) | (string.charCodeAt(i) & 0x3FF));
116         out += (hexTable[0xF0 | (c >> 18)] + hexTable[0x80 | ((c >> 12) & 0x3F)] + hexTable[0x80 | ((c >> 6) & 0x3F)] + hexTable[0x80 | (c & 0x3F)]);
117     }
118
119     return out;
120 };
121
122 exports.compact = function (obj, references) {
123     if (typeof obj !== 'object' || obj === null) {
124         return obj;
125     }
126
127     var refs = references || [];
128     var lookup = refs.indexOf(obj);
129     if (lookup !== -1) {
130         return refs[lookup];
131     }
132
133     refs.push(obj);
134
135     if (Array.isArray(obj)) {
136         var compacted = [];
137
138         for (var i = 0; i < obj.length; ++i) {
139             if (typeof obj[i] !== 'undefined') {
140                 compacted.push(obj[i]);
141             }
142         }
143
144         return compacted;
145     }
146
147     var keys = Object.keys(obj);
148     for (var j = 0; j < keys.length; ++j) {
149         var key = keys[j];
150         obj[key] = exports.compact(obj[key], refs);
151     }
152
153     return obj;
154 };
155
156 exports.isRegExp = function (obj) {
157     return Object.prototype.toString.call(obj) === '[object RegExp]';
158 };
159
160 exports.isBuffer = function (obj) {
161     if (obj === null || typeof obj === 'undefined') {
162         return false;
163     }
164
165     return !!(obj.constructor && obj.constructor.isBuffer && obj.constructor.isBuffer(obj));
166 };