Skip to content

Anysort,符合直觉的多属性排序方法,类型完备,Flexible and Full Typed multi-properties sorter for nested objects

License

Notifications You must be signed in to change notification settings

Lionad-Morotar/Anysort

Repository files navigation

Anysort

Anysort:符合直觉、类型完备的多属性排序方法

  MIT License

Why Anysort

A picture is worth a thousand words.

project moved from Lionad-Morotar/anysort-old

Install

npm install --save anysort-typed

Why Anysort

  • Anysort can sort with multi-attributes
// select articles which has 'it' tag, put ahead,
// then move articles which status is 'editing' at the beginning
anysort(articles)
  .tag.has('it')
  .status.is('editing')
  .map(print)
  • Intuitive
// Array.prototype.sort: what hell the result is!
[].sort.apply([0, '0', 1, 'd', '1', '0', 0, ''])
// ['', 0, '0', '0', 0, 1, '1', 'd']

// Anysort:the result is intuitive
anysort([0, '0', 1, undefined, 'd', '1', '0', null, 0, '', undefined])
// [0, 0, 1, '', '0', '0', '1', 'd']
  • Flexible API
// proxy chain api
anysort(articles).created.date.reverse()

// or
anysort(articles, 'created.date-reverse()')
  • Full typed, even in call-with-string-mode, AMAZING!
// @ts-expect-error
anysort(articles).tag.hass('it')
// @ts-expect-error
anysort(articles, 'created.date-unknownPlugin()')
// OK!
anysort(articles).created.date.reverse()
// OK!
anysort(articles, 'created.date-reverse()')
// @ts-expect-error
anysort(articles).created.date.reverse(123)
// @ts-expect-error
anysort(articles, 'created.date-reverse(123)')
  • Zero dependencies(minified + gzip ≈ 3KB)

  • Well tested, logic and type

  • WIP: Full API document, help wanted

  • WIP: Benchmark, help wanted

Usage

Short instruction。

const posts = getPosts()
const print = (x) => console.log(JSON.stringify(x))

// select articles being edited with IT tags,
// sorted by date in reverse order and time in positive order
anysort(posts, [
  'status-is(editing)',
  'tag-has(it)',
  'created.date-reverse()',
  'created.hour'
]).map(print)

// {"tag":["it"],"status":"editing","created":{"date":"2021-01-02T00:00:00.000Z","hour":23}}
// {"tag":["it"],"status":"editing","created":{"date":"2021-01-01T00:00:00.000Z","hour":16}}
// {"tag":["game","it"],"status":"editing","created":{"date":"2021-01-01T00:00:00.000Z","hour":23}}
// {"tag":["mp3"],"status":"","created":{"date":"2019-08-01T00:00:00.000Z","hour":23}}

// sick of using string manipulation?
// try this!
anysort(getPosts())
  .created.hour.result()
  .created.date.reverse()
  .tag.has('it')
  .status.is('editing')
  .map(print)

// {"tag":["it"],"status":"editing","created":{"date":"2021-01-02T00:00:00.000Z","hour":23}}
// {"tag":["it"],"status":"editing","created":{"date":"2021-01-01T00:00:00.000Z","hour":16}}
// {"tag":["game","it"],"status":"editing","created":{"date":"2021-01-01T00:00:00.000Z","hour":23}}
// {"tag":["mp3"],"status":"","created":{"date":"2019-08-01T00:00:00.000Z","hour":23}}

function getPosts () {
  return [
    {
      tag: ['mp3'],
      status: '',
      created: {
        date: new Date('2019-08-01'),
        hour: 23
      }
    },
    {
      tag: ['game', 'it'],
      status: 'editing',
      created: {
        date: new Date('2021-01-01'),
        hour: 23
      }
    },
    {
      tag: ['it'],
      status: 'editing',
      created: {
        date: new Date('2021-01-01'),
        hour: 16
      }
    },
    {
      tag: ['it'],
      status: 'editing',
      created: {
        date: new Date('2021-01-02'),
        hour: 23
      }
    }
  ]
}

Full API Doc

TODO

Change Log

See ChangeLog.md

Dev & Test

# run test when files change in directory build
npm run watch:test

# modify source code then build
npm run build

How this work

《🌐 Anysort:灵活、优雅的多属性排序》

Pull & Request

See TODO.MD,help wanted!

Related Projects

License

Copyright © 2021, Lionad-Morotar. Released under the MIT License.

About

Anysort,符合直觉的多属性排序方法,类型完备,Flexible and Full Typed multi-properties sorter for nested objects

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published