Code Monkey home page Code Monkey logo

Comments (3)

AliasT avatar AliasT commented on June 10, 2024 1
function find(array) {
  return new S(data);
}

function S(data) {
  this.data = data;
  this.filters = {};
  this.orderings = [];
}

S.prototype.where = function (query) {
  this.filters = Object.assign(
    {},
    this.filters,
    Object.keys(query).reduce(
      // 这里要处理null等特殊值
      (a, c) => ((a[c] = (d) => query[c].test(d[c])), a),
      {}
    )
  );
  return this;
};

S.prototype.orderBy = function (field, desc = "desc") {
  this.orderings.push((a, z) =>
    desc == "desc" ? z[field] - a[field] : a[field] - z[field]
  );

  this.data = this.data.filter((d) =>
    Object.keys(this.filters).every((key) => this.filters[key](d))
  );

  this.orderings.forEach((o) => {
    this.data = this.data.sort(o);
  });

  return this.data;
};

from interview.

zhuanghongbin avatar zhuanghongbin commented on June 10, 2024
function find (origin) {
  return {
    where: function (obj) {
      let r = []
      r = origin.filter(item => {
        return item.title !== null && item.title.toString().search(obj.title) > -1
      })
      return {
        orderBy: function (field, orderTyep = 'desc') {
          let oo = {}
          r.map(o => {
            oo[o[field]] = o
          })
          let result = []
          Object.keys(oo).sort((a, b) => orderTyep === 'desc' ? b - a : a - b).forEach(i => { result.push(oo[i]) })
          return result
        }
      }
    }
  }
}

from interview.

ch3cknull avatar ch3cknull commented on June 10, 2024
function find(array) {
  let data = [...array]

  data.__proto__.where = (obj) => {
    if (data.length == 0) return []
    Object.keys(obj).forEach((key) => {
      // 写法不太优雅,待完善
      if (!data[0].hasOwnProperty(key)) {
        data = []
      } else {
        data = data.filter((s) => obj[key].test(s[key]))
      }
    })
    return data
  }

  data.__proto__.orderBy = (key, sortMode) => {
    if (data.length == 0 || !data[0].hasOwnProperty(key)) return []
    return data.sort((x, y) => {
      return sortMode != 'desc' ? x[key] - y[key] : y[key] - x[key]
    })
  }

  return data
}

同时支持了

find(args)
  .where()
  .where()
  .orderBy()

from interview.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.