MIT License

项目介绍

osteslag/Changeset 是一个托管在 GitHub 上的开源项目,主要使用 Swift 编写,采用 MIT License 许可证,目前获得 800 个 Star、41 次 Fork。

仓库全名
osteslag/Changeset
开源许可证
MIT License
Star 数
800
Fork 数
41
未关闭 Issue
3

README 摘要

Changeset Changeset – pretty awesome little project — Joel Levin This is an attempt at implementing the solution outlined in Dave DeLong’s article, Edit distance and edit steps. A Changeset describes the minimal edits required to go from one Collection of Equatable elements to another. It has been written primarily to be used in conjunction with UITableView and UICollectionView data sources by detecting additions,…

完整内容见 GitHub 原始 README 或本页 。

常见问题

Changeset 用什么语言开发?

主要使用 Swift 编写,采用 MIT License 许可证,目前有 800 个 Star。

Changeset 可以免费商用吗?

仓库采用 MIT License 许可证,可免费使用与修改,商业使用请遵守许可证条款(例如保留版权声明与许可声明)。

如何获取 Changeset 的源码?

可以直接克隆仓库:git clone https://github.com/osteslag/Changeset.git,默认分支为 master;安装与运行步骤见仓库 README。

Changeset 最近还在维护吗?

仓库最近一次更新为 2020年7月28日,当前有 3 个未关闭 Issue;Star 数为 800,可作为社区活跃度的参考。

关于

Minimal edits from one collection to another

描述取自 GitHub 官方 API,中文内容由 GitRead 中文社区 编辑团队整理。

简要分析

热度分
896
价值分
493
活跃状态
活跃

语言占比

Ruby
Swift

Release

README

Changeset

Changeset – pretty awesome little project
— Joel Levin

This is an attempt at implementing the solution outlined in Dave DeLong’s article, Edit distance and edit steps.

A Changeset describes the minimal edits required to go from one Collection of Equatable elements to another.

It has been written primarily to be used in conjunction with UITableView and UICollectionView data sources by detecting additions, deletions, substitutions, and moves between the two sets of data. But it can also be used to compute more general changes between two data sets.

Usage

The following code computes the minimal edits of the canonical example, going from the String collections “kitten” to “sitting”:

let changeset = Changeset(source: "kitten", target: "sitting")

print(changeset)
// 'kitten' -> 'sitting':
//     replace with s at offset 0
//     replace with i at offset 4
//     insert g at offset 6

The following assertion would then succeed:

let edits = [
    Changeset<String>.Edit(operation: .substitution, value: "s", destination: 0),
    Changeset<String>.Edit(operation: .substitution, value: "i", destination: 4),
    Changeset<String>.Edit(operation: .insertion, value: "g", destination: 6),
]
assert(changeset.edits == edits)

If you don’t want the overhead of Changeset itself, which also stores the source and target collections, you can call edits directly (here with example data from Apple’s Table View Programming Guide for iOS):

let source = ["Arizona", "California", "Delaware", "New Jersey", "Washington"]
let target = ["Alaska", "Arizona", "California", "Georgia", "New Jersey", "Virginia"]
let edits = Changeset.edits(from: source, to: target)

print(edits)
// [insert Alaska at offset 0, replace with Georgia at offset 2, replace with Virginia at offset 4]

Note that Changeset uses offsets, not indices, to refer to elements in the collections. This is mainly because Swift collections aren’t guaranteed to use zero-based integer indices. See discussion in issue #37 for more details.

UIKit Integration

The offset values can be used directly in the animation blocks of beginUpdates/endUpdates on UITableView and performBatchUpdates on UICollectionView in that Changeset follows the principles explained under Batch Insertion, Deletion, and Reloading of Rows and Sections in Apple’s guide.

In short; first all deletions and substitutions are made, relative to the source collection, then, relative to the resulting collection, insertions. A move is just a deletion followed by an insertion.

In the iOS framework, two convenience extensions (one on UITableView and one on UICollectionView) have been included to make animated table/collection view updates a breeze. Just call update, like this:

tableView.update(with: changeset.edits)

Custom Comparator

By default a Changeset uses == to compare elements, but you can write your own comparator, illustrated below, where the occurence of an “a” always triggers a change:

let alwaysChangeA: (Character, Character) -> Bool = {
    if $0 == "a" || $1 == "a" {
        return false
    } else {
        return $0 == $1
    }
}
let changeset = Changeset(source: "ab", target: "ab", comparator: alwaysChangeA)

As a result, the changeset will consist of a substitution of the “a” (to another “a”):

let expectedEdits: [Changeset<String>.Edit] = [Changeset.Edit(operation: .substitution, value: "a", destination: 0)]
assert(changeset.edits == expectedEdits)

One possible use of this is when a cell in a UITableView or UICollectionView shouldn’t animate when they change.

Test App

The Xcode project also contains a target to illustrate the usage in an app:

Test App

This uses the extensions mentioned above to animate transitions based on the edits of a Changeset.

License

This project is available under The MIT License.
Copyright © 2015-18, Joachim Bondo. See LICENSE file.

评论

暂无评论