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