anysort-typed
TypeScript icon, indicating that this package has built-in type declarations

3.3.1 • Public • Published

Anysort

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

  MIT License

Why Anysort

A picture is worth a thousand words.

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 begining
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(minifized + 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.

Package Sidebar

Install

npm i anysort-typed

Weekly Downloads

8

Version

3.3.1

License

MIT

Unpacked Size

153 kB

Total Files

20

Last publish

Collaborators

  • lionad