Fanfou Wiki

我等采石之人,当心怀建造大教堂之愿景。

用户工具

站点工具


snippets:js:helpers

差别

这里会显示出您选择的修订版和当前版本之间的差别。

到此差别页面的链接

snippets:js:helpers [2021/01/20 09:00] – 创建 adminsnippets:js:helpers [2021/07/12 12:23] (当前版本) admin
行 2: 行 2:
 /** /**
  * 为数组添加删除方法  * 为数组添加删除方法
- * @param 接收一个数组元素+ * @param {string} b 接收一个数组元素
  * @return boolean  * @return boolean
  */  */
-Array.prototype.remove = function(b) { +Array.prototype.remove = function (b) { 
-  var a = this.indexOf(b);+  const a = this.indexOf(b);
   if (a >= 0) {   if (a >= 0) {
     this.splice(a, 1);     this.splice(a, 1);
行 13: 行 13:
   return false;   return false;
 }; };
 +
 +/**
 + * 随机打乱数组
 + *
 + * @return {Array}
 + */
 +Array.prototype.shuffle = function () {
 +  var input = this;
 +
 +  for (var i = input.length - 1; i >= 0; i--) {
 +    var randomIndex = Math.floor(Math.random() * (i + 1));
 +    var itemAtIndex = input[randomIndex];
 +
 +    input[randomIndex] = input[i];
 +    input[i] = itemAtIndex;
 +  }
 +  return input;
 +};
 +
 /** /**
  * 为window.location 添加查询params的方法  * 为window.location 添加查询params的方法
- * @param 接收一个字符串+ * @param {string} b 接收一个字符串
  * @return null|string  * @return null|string
  */  */
-window.location.query = function(b) { +window.location.query = function (b) { 
-  var reg = new RegExp("(^|&|/?)" + b + "=([^&]*)(&|$)", "i"); +  const reg = new RegExp("(^|&|/?)" + b + "=([^&]*)(&|$)", "i"); 
-  var r = window.location.href.substr(1).match(reg); +  const r = window.location.href.substr(1).match(reg); 
-  if (r != null) return (r[2]);+  if (r != null) return r[2];
   return null;   return null;
 }; };
 +
 +
 +
 /** /**
  * 为window.location 添加查询当前平台的方法  * 为window.location 添加查询当前平台的方法
  * @return null|string  * @return null|string
  */  */
- window.location.platform = function(){ +window.location.platform = function () { 
-    var platform = null; +  let platform = null; 
-    var useragent = navigator.userAgent; +  const useragent = navigator.userAgent; 
-    const platforms = ['Windows NT','Linux','Macintosh','Android','iPhone','iPod','iPad','Windows Phone']; +  // prettier-ignore 
-    for(i=0;i<=platforms.length;i++){ +  const platforms = ["Windows NT""Linux""Macintosh""Android""iPhone""iPod""iPad""Windows Phone"]; 
-        if(useragent.indexOf(platforms[i]) > -1){ +  for (let i = 0; i <= platforms.length; i++) { 
-            platform = platforms[i]; +    if (useragent.indexOf(platforms[i]) > -1) { 
-            break; +      platform = platforms[i]; 
-        }+      break;
     }     }
-    return platform; +  } 
- };+  return platform; 
 +};
 </file> </file>
snippets/js/helpers.1611104418.txt.gz · 最后更改: 2021/01/20 09:00 由 admin