Skip to content

Releases: RougeWare/Swift-Safe-Collection-Access

2.2 - Mutation! 🎉

10 Jun 19:32
a2a2f87
Compare
Choose a tag to compare

Finally, you can use this package to safely mutate collections, as well as accessing them safely as always!

All existing accessors now have mutating companions:

  • [orNil:] taking an index can now set that index, remove that element by setting it to nil, or do nothing if that index lies outside the collection
  • [orNil:] taking an index range can now set that range to the contents of a subsequence, remove the elements at that range by setting it to nil, or do nothing if that index range goes beyond the collection boundaries
  • [clamping:] taking an index can now set that index, remove that element by setting it to nil, or set the closest slot if the index lies outside the collection, or do nothing if the collection is empty

2.2.1

This patch fixes a crash when using this package to address strings and other collections which don't allow you to get an index if it's outside the collection

2.2.2

This patch adds a license to the codebase

2.1.1 - More consistent range behaviors

21 May 17:12
244ed5b
Compare
Choose a tag to compare

What's New

In 1.2.0, the safe range subscripts were introduced. However, at the edges of collections, their behavior was inconsistent with Swift's built-in range subscripts - they would return nil instead of an empty collection.

Now, these have been made the same, returning an empty subsequence, where before this package's versions would return nil:

  • collection[collection.endIndex...] == collection[orNil: collection.endIndex...]
  • collection[..<collection.startIndex] == collection[orNil: ..<collection.startIndex]

Why not 3.0.0?

This change breaks the contract of the API, shouldn't it be a MAJOR version bump?

While that's true, it does so in a backward-compatible way. Any code which was using the 2.0.0 version of these APIs will still work: if it was using a range that 2.0.0 considered valid, it'll still get the same result; if it was using a clearly invalid range, it'll still get nil; the only difference is that if it was using one of the above example edge ranges, it'll now get a non-nil result. Thanks to how Swift thinks about optional values, this will be gracefully handled by any code that does this. Because of this grace, a MAJOR version bump is not necessary. Yay!


Patch Changes

  • 2.1.1 - Fixed typo in comment, improved performance of [orNil: PartialRangeFrom<Index>]

2.0.0 - Range subscripts no longer require `Index: Strideable`

20 May 23:45
df6eca8
Compare
Choose a tag to compare

What's New

Now you can safely subscript a collection using ranges, without requiring that the collection's indices are Strideable by SignedIntegers.

This also comes with improvements to these subscripts to make them all O(1)!

To achieve this, now collections with non-contiguous indices (like Sets) might not behave the same as before. Because of that, this is technically incompatible with the old version so, it's 2.0.0 instead of 1.3.0.

What's Old

[safe:] has been removed, for both improved clarity and reduced tech debt. Instead, use [orNil:] for the same behavior, or [clamping:] if that's the best safe behavior for you.

1.2.0 - Support for ranges, deprecated `[safe:]`

20 May 22:56
4d1ce51
Compare
Choose a tag to compare

What's New

You can now use these kinds of ranges to safely access any elements in any random-access collection whose indices are strideable (Most Foundation collections are this way):

  • Range
  • ClosedRange
  • PartialRangeFrom
  • PartialRangeUpTo
  • PartialRangeUpThrough

What's Old

I chose to deprecate [safe:]. It was solely an alias to [orNil:], and its name describes the whole point of every API this package, thus making it nondescript to the point of reducing clarity. Also, it would have required each [orNil:] API to be duplicated to a [safe:] one. Removing it increases clarity and decreases tech debt. It will be removed eventually, in SafeCollectionAccess 2.0.0.

1.1.0 - `[clamping:]`

10 Dec 08:19
990d5d0
Compare
Choose a tag to compare

Like [orNil:], this won't crash when you access indices outside the collection. However, unlike [orNil:], instead of returning nil for those, [clamping:] will return the element at the closest extreme. If the collection is empty, then there is no such extreme, and so it will return nil.

1.0.1 - MVP

07 Dec 09:51
b99bb75
Compare
Choose a tag to compare

Access any index in a RandomAccessCollection (including Arrays) but it won't crash if you're out of bounds!


Patch changes:

  • 1.0.1
    • Added the subscript labels to the examples in the readme. Can't believe I forgot that 😅