SwiftFormats

main

SwiftFormats
schwa/SwiftFormats

SwiftFormats

More FormatStyle implementation for more types.

Description

This package provides more FormatStyle implementations for various types. Many types also provide parserStrategy implementations for parsing strings into the type where appropriate.

It also provides extensions for String and string interpolation to make it easier to format values.

Types

Name In (1) Out (2) Format (3) Parser (4) Accessor (5) Notes
BinaryFloatingPoint String Angles Yes angle Radians, degrees, etc
BinaryFloatingPoint String Degree Minute Seconds No dmsNotation
CGPoint String List (6) Yes point
ClosedRange String X ... Y Yes No
CLLocationCoordinate2D String List No coordinates
BinaryFloatingPoint String Latitude No latitude Including hemisphere
BinaryFloatingPoint String Longitude No longitude Including hemisphere
Any String Description No describing Uses String(describing:)
Any String Dump No dumped Uses dump()
DataProtocol String Hex-dumped No hexdumped
Codable String JSON Yes json Uses JSONEncoder and JSONDecoder
SIMD3<Float> String List or mapping Yes vector
SIMD matrix types String List Yes matrix
BinaryInteger String Radixed format No Various Binary, Octal, Hex representations

Notes

1: Type provided as FormatInput in the FormatStyle implementation. 2: Type provided as FormatOutput in the FormatStyle implementation. 3: Format of the output. 4: Whether the FormatStyle implementation provides a corresponding ParserStrategy. 5: Whether a convenience property is provided to access style on FormatStyle. 6: Formats the input as a comma-separated list.

Examples

String interpolation

let number = 123.456
let formatted = "The number is \(number, .number)"
// formatted == "The number is 123.456"

CoreGraphics

let point = CGPoint(x: 1.234, y: 5.678)
let formatted = point.formatted(.decimal(places: 2))
// formatted == "(1.23, 5.68)"

Future Plans

The initial priority is to expose formats and parsers for more SIMD/CG types. Some common helper format styles will be added (e.g. "field" parser - see TODO).

TODOs

  • Find and handle all the TODOs
  • Clean up the parser init methods. Foundation parsers do not have public .init() methods and are only created via corresponding FormatStyles. Follow this. This will be API breaking.
  • Add more .formatted() and .formatted(_ style:) functions where appropriate
    • Track this in tabel
  • Add sugar for parsing (does a standard library equivalent to .formatted() exist?)
  • Investigate attribute strings and other non-string FormatOutput types
  • More CoreGraphics types
  • Yet another CGColor to web colour converter
  • Do all SIMD types in a sane way
  • Make a "field" type generic format, e.g. represent CGPoint as x: 1.234, y: 5.678 (use for SIMD and other CG types)
  • A parser for angle would be nice but Measurement<Angle> has no parser we can base it off.
  • Investigate a "Parsable" and "Formattable" protocol that provides a .formatted() etc functions.
  • Add support for SwiftUI.Angle - how do we differentiate between two different Angle formatters?
  • Add support for Spatial.framework types
  • Make quaternion and vector parsers more accepting of styles.
  • Make quaternion parser treat angles as angle foramts (i.e. with °)
  • Make angle parser not care and parse rad or ° correctly.
  • Test 'radian' vs 'radians' vs localized.
  • In general parsers need to be configured less and accept more formats.
  • Real docc documentation.
  • Fuzz parsers and see what fun will ensue.

Resources

Apple

Other

Description

  • Swift Tools 5.7.0
View More Packages from this Author

Dependencies

Last updated: Sat Apr 13 2024 15:11:04 GMT-0900 (Hawaii-Aleutian Daylight Time)