Anysort:符合直觉、类型完备的多属性排序方法
A picture is worth a thousand words.
project moved from Lionad-Morotar/anysort-old
npm install --save anysort-typed
- 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
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
}
}
]
}
TODO
See ChangeLog.md
# run test when files change in directory build
npm run watch:test
# modify source code then build
npm run build
See TODO.MD,help wanted!
Copyright © 2021, Lionad-Morotar. Released under the MIT License.