alasql 2.1.3 → 2.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/alasql-cli.js CHANGED
@@ -44,6 +44,14 @@ let yargs = require('yargs')
44
44
  .nargs('f', 1)
45
45
  .normalize('f')
46
46
 
47
+ .boolean('ast')
48
+ .describe('ast', 'Print AST instead of result')
49
+ .normalize('ast')
50
+
51
+ /*.boolean('comp')
52
+ .describe('comp', 'Print compiled function instead of result')
53
+ .normalize('comp')*/
54
+
47
55
  .help('h')
48
56
  .alias('h', 'help')
49
57
 
@@ -115,6 +123,26 @@ function execute(sql, params) {
115
123
  }
116
124
  }
117
125
 
126
+ if (argv.ast) {
127
+ try {
128
+ console.log(formatOutput(alasql.parse(sql, params)));
129
+ process.exit(0);
130
+ } catch (e) {
131
+ console.error(e);
132
+ process.exit(1);
133
+ }
134
+ }
135
+
136
+ /*if (argv.comp) {
137
+ try {
138
+ console.log(alasql.compile(sql, params));
139
+ process.exit(0);
140
+ } catch (e) {
141
+ console.error(e);
142
+ process.exit(1);
143
+ }
144
+ }*/
145
+
118
146
  alasql
119
147
  .promise(sql, params)
120
148
  .then(function (res) {
@@ -1,7 +1,7 @@
1
- //! AlaSQL v2.1.2-develop-e3d1ac82 | © 2014-2022 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v2.1.4-develop-5f5b471d | © 2014-2022 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 2.1.2-develop-e3d1ac82
4
+ @version 2.1.4-develop-5f5b471d
5
5
 
6
6
  AlaSQL - JavaScript SQL database
7
7
  © 2014-2016 Andrey Gershun & Mathias Rangel Wulff
@@ -1,2 +1,2 @@
1
- //! AlaSQL v2.1.2-develop-e3d1ac82 | © 2014-2022 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v2.1.4-develop-5f5b471d | © 2014-2022 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  !function(r,e){"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?module.exports=e():r.alasql=e()}(this,function(){function i(r,e,o){"function"==typeof(e=e||[])&&(scope=o,o=e,e=[]),"object"!=typeof e&&(e=[e]);var t=i.lastid++;i.buffer[t]=o,i.webworker.postMessage({id:t,sql:r,params:e})}var a;if(i.options={},i.options.progress=function(){},isArray=function(r){return"[object Array]"===Object.prototype.toString.call(r)},i.promise=function(){throw new Error("Please include a Promise/A+ library")},"undefined"!=typeof Promise&&(a=function(r,e,n,s){return new Promise(function(o,t){i(r,e,function(r,e){e?t(e):(n&&s&&!1!==i.options.progress&&i.options.progress(n,s),o(r))})})},i.promise=function(r,e){if("undefined"==typeof Promise)throw new Error("Please include a Promise/A+ library");if("string"==typeof r)return a(r,e);if(!isArray(r)||r.length<1||void 0!==e)throw new Error("Error in .promise parameters");var o=r;if(!(o.length<1)){for(var t,n,s=[],i=0;i<o.length;i++){if("string"==typeof(n=o[i])&&(n=[n]),!isArray(n)||n.length<1||2<n.length)throw new Error("Error in .promise parameter");t=n[0],n=n[1]||void 0,s.push(a(t,n,i,o.length))}return Promise.all(s)}}),i=i||!1)return i.worker=function(){throw new Error("Can find webworker in this enviroment")},"undefined"!=typeof Worker&&(i.worker=function(r,e,o){if(void 0===(r=!0===r?void 0:r))for(var t=document.getElementsByTagName("script"),n=0;n<t.length;n++){if("alasql-worker.js"===t[n].src.substr(-16).toLowerCase()){r=t[n].src.substr(0,t[n].src.length-16)+"alasql.js";break}if("alasql-worker.min.js"===t[n].src.substr(-20).toLowerCase()){r=t[n].src.substr(0,t[n].src.length-20)+"alasql.min.js";break}if("alasql.js"===t[n].src.substr(-9).toLowerCase()){r=t[n].src;break}if("alasql.min.js"===t[n].src.substr(-13).toLowerCase()){r=t[n].src.substr(0,t[n].src.length-13)+"alasql.min.js";break}}if(void 0===r)throw new Error("Path to alasql.js is not specified");var s;!1!==r?(s=(s="importScripts('")+r+"');self.onmessage = function(event) {alasql(event.data.sql,event.data.params, function(data){postMessage({id:event.data.id, data:data});});}",s=new Blob([s],{type:"text/plain"}),i.webworker=new Worker(URL.createObjectURL(s)),i.webworker.onmessage=function(r){var e=r.data.id;i.buffer[e](r.data.data),delete i.buffer[e]},i.webworker.onerror=function(r){throw r},1<arguments.length&&i("REQUIRE "+e.map(function(r){return'"'+r+'"'}).join(","),[],o)):!1===r&&delete i.webworker}),i.lastid=0,i.buffer={},i.worker(),i;throw new Error("alasql was not found")});
package/dist/alasql.fs.js CHANGED
@@ -1,7 +1,7 @@
1
- //! AlaSQL v2.1.2-develop-e3d1ac82 | © 2014-2022 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v2.1.4-develop-5f5b471d | © 2014-2022 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 2.1.2-develop-e3d1ac82
4
+ @version 2.1.4-develop-5f5b471d
5
5
 
6
6
  AlaSQL - JavaScript SQL database
7
7
  © 2014-2016 Andrey Gershun & Mathias Rangel Wulff
@@ -142,7 +142,7 @@ var alasql = function(sql, params, cb, scope) {
142
142
  Current version of alasql
143
143
  @constant {string}
144
144
  */
145
- alasql.version = '2.1.2-develop-e3d1ac82';
145
+ alasql.version = '2.1.4-develop-5f5b471d';
146
146
 
147
147
  /**
148
148
  Debug flag
@@ -3606,7 +3606,14 @@ var loadFile = (utils.loadFile = function (path, asy, success, error) {
3606
3606
  @todo merge functionality from loadFile and LoadBinaryFile
3607
3607
  */
3608
3608
 
3609
- var loadBinaryFile = (utils.loadBinaryFile = function (path, asy, success, error) {
3609
+ var loadBinaryFile = (utils.loadBinaryFile = function (
3610
+ path,
3611
+ runAsync,
3612
+ success,
3613
+ error = (x) => {
3614
+ throw x;
3615
+ }
3616
+ ) {
3610
3617
  var fs;
3611
3618
  if (utils.isNode || utils.isMeteorServer) {
3612
3619
  //*not-for-browser/*
@@ -3616,7 +3623,7 @@ var loadBinaryFile = (utils.loadBinaryFile = function (path, asy, success, error
3616
3623
  var request = require('request');
3617
3624
  request({url: path, encoding: null}, function (err, response, data) {
3618
3625
  if (err) {
3619
- throw err;
3626
+ return error(err);
3620
3627
  }
3621
3628
  var arr = [];
3622
3629
  for (var i = 0; i < data.length; ++i) {
@@ -3625,10 +3632,10 @@ var loadBinaryFile = (utils.loadBinaryFile = function (path, asy, success, error
3625
3632
  success(arr.join(''));
3626
3633
  });
3627
3634
  } else {
3628
- if (asy) {
3635
+ if (runAsync) {
3629
3636
  fs.readFile(path, function (err, data) {
3630
3637
  if (err) {
3631
- throw err;
3638
+ return error(err);
3632
3639
  }
3633
3640
  var arr = [];
3634
3641
  for (var i = 0; i < data.length; ++i) {
@@ -3637,7 +3644,12 @@ var loadBinaryFile = (utils.loadBinaryFile = function (path, asy, success, error
3637
3644
  success(arr.join(''));
3638
3645
  });
3639
3646
  } else {
3640
- var data = fs.readFileSync(path);
3647
+ var data;
3648
+ try {
3649
+ data = fs.readFileSync(path);
3650
+ } catch (e) {
3651
+ return error(e);
3652
+ }
3641
3653
  var arr = [];
3642
3654
  for (var i = 0; i < data.length; ++i) {
3643
3655
  arr[i] = String.fromCharCode(data[i]);
@@ -3651,16 +3663,18 @@ var loadBinaryFile = (utils.loadBinaryFile = function (path, asy, success, error
3651
3663
  var RNFetchBlob = require('react-native-fetch-blob').default;
3652
3664
  var dirs = RNFetchBlob.fs.dirs;
3653
3665
  //should use readStream instead if the file is large
3654
- RNFetchBlob.fs.readFile(path, 'base64').then(function (data) {
3655
- //RNFetchBlob.base64.decode(data) //need more test on excel
3656
- success(data);
3657
- });
3666
+ RNFetchBlob.fs
3667
+ .readFile(path, 'base64')
3668
+ .then(function (data) {
3669
+ success(data);
3670
+ })
3671
+ .catch(error);
3658
3672
  //*/
3659
3673
  } else {
3660
3674
  if (typeof path === 'string') {
3661
3675
  // For browser
3662
3676
  var xhr = new XMLHttpRequest();
3663
- xhr.open('GET', path, asy); // Async
3677
+ xhr.open('GET', path, runAsync); // Async
3664
3678
  xhr.responseType = 'arraybuffer';
3665
3679
  xhr.onload = function () {
3666
3680
  var data = new Uint8Array(xhr.response);
@@ -3670,7 +3684,7 @@ var loadBinaryFile = (utils.loadBinaryFile = function (path, asy, success, error
3670
3684
  }
3671
3685
  success(arr.join(''));
3672
3686
  };
3673
- // xhr.responseType = "blob";
3687
+ xhr.onerror = error;
3674
3688
  xhr.send();
3675
3689
  } else if (path instanceof Event) {
3676
3690
 
@@ -3681,6 +3695,7 @@ var loadBinaryFile = (utils.loadBinaryFile = function (path, asy, success, error
3681
3695
  var data = e.target.result;
3682
3696
  success(data);
3683
3697
  };
3698
+ reader.onerror = error();
3684
3699
  reader.readAsArrayBuffer(files[0]);
3685
3700
  } else if (path instanceof Blob) {
3686
3701
  success(path);
@@ -4536,6 +4551,10 @@ alasql.options.excel = {cellDates: true};
4536
4551
 
4537
4552
  alasql.options.joinstar = 'overwrite'; // Option for SELECT * FROM a,b
4538
4553
 
4554
+ alasql.options.loopbreak = 100000;
4555
+
4556
+ alasql.options;
4557
+
4539
4558
  //alasql.options.worker = false;
4540
4559
 
4541
4560
  // Variables
@@ -5682,7 +5701,7 @@ function doSearch(databaseid, params, cb) {
5682
5701
  // throw new Error('Selector "'+sel.srchid+'" not found');
5683
5702
  // };
5684
5703
 
5685
- var SECURITY_BREAK = 100000;
5704
+ var INFINITE_LOOP_BREAK = alasql.options.loopbreak || 100000;
5686
5705
 
5687
5706
  if (sel.selid) {
5688
5707
  // TODO Process Selector
@@ -5913,8 +5932,8 @@ function doSearch(databaseid, params, cb) {
5913
5932
  }
5914
5933
  // Security brake
5915
5934
  i++;
5916
- if (i > SECURITY_BREAK) {
5917
- throw new Error('Security brake. Number of iterations = ' + i);
5935
+ if (i > INFINITE_LOOP_BREAK) {
5936
+ throw new Error('Infinite loop brake. Number of iterations = ' + i);
5918
5937
  }
5919
5938
  }
5920
5939
  }
@@ -6094,8 +6113,8 @@ function doSearch(databaseid, params, cb) {
6094
6113
 
6095
6114
  // Security brake
6096
6115
  i++;
6097
- if (i > SECURITY_BREAK) {
6098
- throw new Error('Security brake. Number of iterations = ' + i);
6116
+ if (i > INFINITE_LOOP_BREAK) {
6117
+ throw new Error('Infinite loop brake. Number of iterations = ' + i);
6099
6118
  }
6100
6119
  }
6101
6120
  return retval;
@@ -6129,8 +6148,8 @@ function doSearch(databaseid, params, cb) {
6129
6148
 
6130
6149
  // Security brake
6131
6150
  i++;
6132
- if (i > SECURITY_BREAK) {
6133
- throw new Error('Loop brake. Number of iterations = ' + i);
6151
+ if (i > INFINITE_LOOP_BREAK) {
6152
+ throw new Error('Infinite loop brake. Number of iterations = ' + i);
6134
6153
  }
6135
6154
  }
6136
6155
 
@@ -11483,7 +11502,7 @@ yy.AggrValue.prototype.toType = function () {
11483
11502
  return this.expression.toType();
11484
11503
  }
11485
11504
 
11486
- // todo: implement default;
11505
+ return this.expression.toType();
11487
11506
  };
11488
11507
 
11489
11508
  yy.AggrValue.prototype.toJS = function (/*context, tableid, defcols*/) {
@@ -11934,42 +11953,39 @@ alasql.aggr.GROUP_CONCAT = function (v, s, stage) {
11934
11953
  return s;
11935
11954
  };
11936
11955
 
11937
- alasql.aggr.MEDIAN = function (v, s, stage) {
11956
+ alasql.aggr.median = alasql.aggr.MEDIAN = function (v, s, stage) {
11938
11957
  if (stage === 2) {
11939
11958
  if (v !== null) {
11940
11959
  s.push(v);
11941
11960
  }
11942
11961
  return s;
11943
- } else if (stage === 1) {
11962
+ }
11963
+
11964
+ if (stage === 1) {
11944
11965
  if (v === null) {
11945
11966
  return [];
11946
11967
  }
11947
11968
  return [v];
11948
- } else {
11949
- if (!s.length) {
11950
- return s;
11951
- }
11969
+ }
11952
11970
 
11953
- var r = s.sort(function (a, b) {
11954
- if (a === b) {
11955
- return 0;
11956
- }
11957
- if (a > b) {
11958
- return 1;
11959
- }
11960
- return -1;
11961
- });
11962
- var p = (r.length + 1) / 2;
11963
- if (Number.isInteger(p)) {
11964
- return r[p - 1];
11965
- }
11971
+ if (!s.length) {
11972
+ return null;
11973
+ }
11966
11974
 
11967
- var value = r[Math.floor(p - 1)];
11968
- if (typeof value !== 'number' && !(value instanceof Number)) {
11969
- return value;
11970
- }
11975
+ var r = s.sort((a, b) => {
11976
+ if (a > b) return 1;
11977
+ if (a < b) return -1;
11978
+ return 0;
11979
+ });
11971
11980
 
11972
- return (value + r[Math.ceil(p - 1)]) / 2;
11981
+ var middle = (r.length + 1) / 2;
11982
+ var middleFloor = middle | 0;
11983
+ var el = r[middleFloor - 1];
11984
+
11985
+ if (middle === middleFloor || (typeof el !== 'number' && !(el instanceof Number))) {
11986
+ return el;
11987
+ } else {
11988
+ return (el + r[middleFloor]) / 2;
11973
11989
  }
11974
11990
  };
11975
11991
 
package/dist/alasql.js CHANGED
@@ -1,7 +1,7 @@
1
- //! AlaSQL v2.1.2-develop-e3d1ac82 | © 2014-2022 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v2.1.4-develop-5f5b471d | © 2014-2022 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 2.1.2-develop-e3d1ac82
4
+ @version 2.1.4-develop-5f5b471d
5
5
 
6
6
  AlaSQL - JavaScript SQL database
7
7
  © 2014-2016 Andrey Gershun & Mathias Rangel Wulff
@@ -142,7 +142,7 @@ var alasql = function(sql, params, cb, scope) {
142
142
  Current version of alasql
143
143
  @constant {string}
144
144
  */
145
- alasql.version = '2.1.2-develop-e3d1ac82';
145
+ alasql.version = '2.1.4-develop-5f5b471d';
146
146
 
147
147
  /**
148
148
  Debug flag
@@ -3605,7 +3605,14 @@ var loadFile = (utils.loadFile = function (path, asy, success, error) {
3605
3605
  @todo merge functionality from loadFile and LoadBinaryFile
3606
3606
  */
3607
3607
 
3608
- var loadBinaryFile = (utils.loadBinaryFile = function (path, asy, success, error) {
3608
+ var loadBinaryFile = (utils.loadBinaryFile = function (
3609
+ path,
3610
+ runAsync,
3611
+ success,
3612
+ error = (x) => {
3613
+ throw x;
3614
+ }
3615
+ ) {
3609
3616
  var fs;
3610
3617
  if (utils.isNode || utils.isMeteorServer) {
3611
3618
  /*not-for-browser/*
@@ -3615,7 +3622,7 @@ var loadBinaryFile = (utils.loadBinaryFile = function (path, asy, success, error
3615
3622
  var request = require('request');
3616
3623
  request({url: path, encoding: null}, function (err, response, data) {
3617
3624
  if (err) {
3618
- throw err;
3625
+ return error(err);
3619
3626
  }
3620
3627
  var arr = [];
3621
3628
  for (var i = 0; i < data.length; ++i) {
@@ -3624,10 +3631,10 @@ var loadBinaryFile = (utils.loadBinaryFile = function (path, asy, success, error
3624
3631
  success(arr.join(''));
3625
3632
  });
3626
3633
  } else {
3627
- if (asy) {
3634
+ if (runAsync) {
3628
3635
  fs.readFile(path, function (err, data) {
3629
3636
  if (err) {
3630
- throw err;
3637
+ return error(err);
3631
3638
  }
3632
3639
  var arr = [];
3633
3640
  for (var i = 0; i < data.length; ++i) {
@@ -3636,7 +3643,12 @@ var loadBinaryFile = (utils.loadBinaryFile = function (path, asy, success, error
3636
3643
  success(arr.join(''));
3637
3644
  });
3638
3645
  } else {
3639
- var data = fs.readFileSync(path);
3646
+ var data;
3647
+ try {
3648
+ data = fs.readFileSync(path);
3649
+ } catch (e) {
3650
+ return error(e);
3651
+ }
3640
3652
  var arr = [];
3641
3653
  for (var i = 0; i < data.length; ++i) {
3642
3654
  arr[i] = String.fromCharCode(data[i]);
@@ -3650,16 +3662,17 @@ var loadBinaryFile = (utils.loadBinaryFile = function (path, asy, success, error
3650
3662
  var RNFetchBlob = require('react-native-fetch-blob')["default"];
3651
3663
  var dirs = RNFetchBlob.fs.dirs;
3652
3664
  //should use readStream instead if the file is large
3653
- RNFetchBlob.fs.readFile(path, 'base64').then(function (data) {
3654
- //RNFetchBlob.base64.decode(data) //need more test on excel
3655
- success(data);
3656
- });
3665
+ RNFetchBlob.fs
3666
+ .readFile(path, 'base64')
3667
+ .then(function (data) {
3668
+ success(data);
3669
+ })["catch"](error);
3657
3670
  //*/
3658
3671
  } else {
3659
3672
  if (typeof path === 'string') {
3660
3673
  // For browser
3661
3674
  var xhr = new XMLHttpRequest();
3662
- xhr.open('GET', path, asy); // Async
3675
+ xhr.open('GET', path, runAsync); // Async
3663
3676
  xhr.responseType = 'arraybuffer';
3664
3677
  xhr.onload = function () {
3665
3678
  var data = new Uint8Array(xhr.response);
@@ -3669,7 +3682,7 @@ var loadBinaryFile = (utils.loadBinaryFile = function (path, asy, success, error
3669
3682
  }
3670
3683
  success(arr.join(''));
3671
3684
  };
3672
- // xhr.responseType = "blob";
3685
+ xhr.onerror = error;
3673
3686
  xhr.send();
3674
3687
  } else if (path instanceof Event) {
3675
3688
 
@@ -3680,6 +3693,7 @@ var loadBinaryFile = (utils.loadBinaryFile = function (path, asy, success, error
3680
3693
  var data = e.target.result;
3681
3694
  success(data);
3682
3695
  };
3696
+ reader.onerror = error();
3683
3697
  reader.readAsArrayBuffer(files[0]);
3684
3698
  } else if (path instanceof Blob) {
3685
3699
  success(path);
@@ -4531,6 +4545,10 @@ alasql.options.excel = {cellDates: true};
4531
4545
 
4532
4546
  alasql.options.joinstar = 'overwrite'; // Option for SELECT * FROM a,b
4533
4547
 
4548
+ alasql.options.loopbreak = 100000;
4549
+
4550
+ alasql.options;
4551
+
4534
4552
  //alasql.options.worker = false;
4535
4553
 
4536
4554
  // Variables
@@ -5677,7 +5695,7 @@ function doSearch(databaseid, params, cb) {
5677
5695
  // throw new Error('Selector "'+sel.srchid+'" not found');
5678
5696
  // };
5679
5697
 
5680
- var SECURITY_BREAK = 100000;
5698
+ var INFINITE_LOOP_BREAK = alasql.options.loopbreak || 100000;
5681
5699
 
5682
5700
  if (sel.selid) {
5683
5701
  // TODO Process Selector
@@ -5908,8 +5926,8 @@ function doSearch(databaseid, params, cb) {
5908
5926
  }
5909
5927
  // Security brake
5910
5928
  i++;
5911
- if (i > SECURITY_BREAK) {
5912
- throw new Error('Security brake. Number of iterations = ' + i);
5929
+ if (i > INFINITE_LOOP_BREAK) {
5930
+ throw new Error('Infinite loop brake. Number of iterations = ' + i);
5913
5931
  }
5914
5932
  }
5915
5933
  }
@@ -6089,8 +6107,8 @@ function doSearch(databaseid, params, cb) {
6089
6107
 
6090
6108
  // Security brake
6091
6109
  i++;
6092
- if (i > SECURITY_BREAK) {
6093
- throw new Error('Security brake. Number of iterations = ' + i);
6110
+ if (i > INFINITE_LOOP_BREAK) {
6111
+ throw new Error('Infinite loop brake. Number of iterations = ' + i);
6094
6112
  }
6095
6113
  }
6096
6114
  return retval;
@@ -6124,8 +6142,8 @@ function doSearch(databaseid, params, cb) {
6124
6142
 
6125
6143
  // Security brake
6126
6144
  i++;
6127
- if (i > SECURITY_BREAK) {
6128
- throw new Error('Loop brake. Number of iterations = ' + i);
6145
+ if (i > INFINITE_LOOP_BREAK) {
6146
+ throw new Error('Infinite loop brake. Number of iterations = ' + i);
6129
6147
  }
6130
6148
  }
6131
6149
 
@@ -11478,7 +11496,7 @@ yy.AggrValue.prototype.toType = function () {
11478
11496
  return this.expression.toType();
11479
11497
  }
11480
11498
 
11481
- // todo: implement default;
11499
+ return this.expression.toType();
11482
11500
  };
11483
11501
 
11484
11502
  yy.AggrValue.prototype.toJS = function (/*context, tableid, defcols*/) {
@@ -11929,42 +11947,39 @@ alasql.aggr.GROUP_CONCAT = function (v, s, stage) {
11929
11947
  return s;
11930
11948
  };
11931
11949
 
11932
- alasql.aggr.MEDIAN = function (v, s, stage) {
11950
+ alasql.aggr.median = alasql.aggr.MEDIAN = function (v, s, stage) {
11933
11951
  if (stage === 2) {
11934
11952
  if (v !== null) {
11935
11953
  s.push(v);
11936
11954
  }
11937
11955
  return s;
11938
- } else if (stage === 1) {
11956
+ }
11957
+
11958
+ if (stage === 1) {
11939
11959
  if (v === null) {
11940
11960
  return [];
11941
11961
  }
11942
11962
  return [v];
11943
- } else {
11944
- if (!s.length) {
11945
- return s;
11946
- }
11963
+ }
11947
11964
 
11948
- var r = s.sort(function (a, b) {
11949
- if (a === b) {
11950
- return 0;
11951
- }
11952
- if (a > b) {
11953
- return 1;
11954
- }
11955
- return -1;
11956
- });
11957
- var p = (r.length + 1) / 2;
11958
- if (Number.isInteger(p)) {
11959
- return r[p - 1];
11960
- }
11965
+ if (!s.length) {
11966
+ return null;
11967
+ }
11961
11968
 
11962
- var value = r[Math.floor(p - 1)];
11963
- if (typeof value !== 'number' && !(value instanceof Number)) {
11964
- return value;
11965
- }
11969
+ var r = s.sort((a, b) => {
11970
+ if (a > b) return 1;
11971
+ if (a < b) return -1;
11972
+ return 0;
11973
+ });
11966
11974
 
11967
- return (value + r[Math.ceil(p - 1)]) / 2;
11975
+ var middle = (r.length + 1) / 2;
11976
+ var middleFloor = middle | 0;
11977
+ var el = r[middleFloor - 1];
11978
+
11979
+ if (middle === middleFloor || (typeof el !== 'number' && !(el instanceof Number))) {
11980
+ return el;
11981
+ } else {
11982
+ return (el + r[middleFloor]) / 2;
11968
11983
  }
11969
11984
  };
11970
11985