preface

We often have similar features and requirements when developing front-end projects. If a lot of CTRL + C and CTRL + V can create a lot of code redundancy, a good solution is to have a common library of functions and styles that we can refer to when we use them. Here I collected some commonly used reusable JS and CSS to share with you.

Comprehensive type judgment

Because Typeof can’t distinguish between an array and an object, it’s more accurate to use toString

function getType(data) {
  var toString = Object.prototype.toString;
  var dataType = data instanceof Element ?
    "element" // To unify the DOM node type output
    :
    toString.call(data).replace(/\[object\s(.+)\]/."$1").toLowerCase()
  return dataType
};
Copy the code

Array to heavy

In THE case of ES6, using array. from(new Set(arr)) would be more concise

function distinctArray(val) {
  if (!Array.isArray(val)) {
    console.log('type error! ');
    return;
  }

  var array = [];
  for (var i = 0; i < val.length; i++) {
    if (array.indexOf(val[i]) === -1) { array.push(val[i]); }}return array;
}
Copy the code

Intercept the parameters after the path and turn it into a JSON object

function getRequest() {
  var url = decodeURI(decodeURI(location.search)); // Get the "?" in the URL. The string after the character is decodeRUI decoded twice
  var theRequest = new Object(a);if (url.indexOf("?") != -1) {
    var str = url.substr(1);
    var strs = str.split("&");
    for (var i = 0; i < strs.length; i++) {
      theRequest[strs[i].split("=") [0]] = (strs[i].split("=") [1]); }}return theRequest;
}
Copy the code

Date adds custom function, time format conversion

Date.prototype.format = function (fmt) {
  var o = {
    "M+": this.getMonth() + 1./ / in
    "d+": this.getDate(), / /,
    "h+": this.getHours(), / / hour
    "m+": this.getMinutes(), / / points
    "s+": this.getSeconds(), / / SEC.
    "q+": Math.floor((this.getMonth() + 3) / 3), / / quarter
    "S": this.getMilliseconds() / / ms
  };
  if(! fmt) { fmt ='yyyyMMddhhmmss';
  }
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp. $1,this.getFullYear() + "").substr(4 - RegExp.$1.length));
  }
  for (var k in o) {
    if (new RegExp("(" + k + ")").test(fmt)) {
      fmt = fmt.replace(RegExp. $1,RegExp.$1.length == 1)? (o[k]) : (("00" + o[k]).substr((""+ o[k]).length))); }}return fmt;
}
Copy the code

Get the day of the week

function getWeek(date, fmt) {
  fmt = fmt || 'week'
  // Bar conversion is not supported by iOS
  date = date.replace(/-/g.'/');
  // Check whether date is a time format
  if (date && new Date(date) ! ='Invalid Date') {
    var week = new Date(date).getDay();
    if (week == 0) {
      fmt += "Day";
    } else if (week == 1) {
      fmt += "一";
    } else if (week == 2) {
      fmt += "二";
    } else if (week == 3) {
      fmt += "Three";
    } else if (week == 4) {
      fmt += "Four";
    } else if (week == 5) {
      fmt += "Five";
    } else if (week == 6) {
      fmt = "Six"; }}else {
    fmt = 'Abnormal time format'
  }
  return fmt
}
Copy the code