Swiftpack.co - cameronshemilt/BetterToStrings as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by cameronshemilt.
cameronshemilt/BetterToStrings 0.2.1
Easily convert dates and numbers to specific string formats
⭐️ 2
🕓 2 years ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/cameronshemilt/BetterToStrings.git", from: "0.2.1")

BetterToStrings

Installation

Swift Package Manager

Add https://github.com/cameronshemilt/BetterToStrings to your Package Dependencies.

Usage

This Package offers improved toString() methods on the following Types:

Date

Use toString(format: String) on a Date to receive the corresponding string to your format.

The format is the same as you would use in a DateFormatter(). An exact guide can be found here.

Preset Formats

This Package also offers some Date Format presets. You can use them via the DateFormat-enum as an optional convenience method: toString(_ dateFormat: DateFormat).

Case Date Format Example Output
.debug "YYYY-MM-dd HH:mm:ss Z" 1970-01-01 00:00:00 +0000
.date "YYYY-MM-dd" 1970-01-01
.europeanDate "dd.MM.YYYY" 01.01.1970
.americanDate "MM/dd/YYYY" 01/01/1970
.spelledDate "dd. MMMM YYYY" 01. January 1970
.spelledDateShort "dd. MMM. YYYY" 01. Jan. 1970
.time "HH:mm:ss" 00:00:00
.meridiemTime "hh:mm:ss a" 00:00:00 AM
.timeZone "Z (zzzz)" +0000 (Greenwich Mean Time)

Smart Conversion

All toString() methods for Date have a paramterer smartConbersion: Bool. This is set to false by default.

Smart conversion automatically returns "Today", "Yesterday" and "Tomorrow" if applicable. Should the Date be within one week of the current Date, the weekday will be returned (eg. "Monday", "Tuesday" etc.) .

Numbers

Supported numbers are Int, Float and Double.

The function toString(abbreviation: AbbreviationStyle, maxSize: Int?, maxFractionDigits: Int?) allows you to customise the resulting String with the following parameters:

Parameter Type Default Value Description
abbreviation AbbreviationStyle .none The abbreviation style. More information can be found further below.
maxSize Int? nil The maximum count of characters the resulting string may have. This includes -, K, M etc., but excludes the decimal point.
maxFractionDigits Int? nil The maximum count of fraction digits the resulting String may have.

When maxSize or maxFractionDigits are set to nil, the size/number of fraction digits will be infinite.

Abbreviation Styles

Case Symbol Description
.none none Does not alter the number.
.thousand K Displays the number in thousands and appends a K.
.million M Displays the number in millions and appends a M.
.intelligent Depending Automatically switches the abbreviation style, depending on the size of the number.

GitHub

link
Stars: 2
Last commit: 2 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

Version 0.2.1
2 years ago
  • Fix various bugs

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics