Skip to content

Commit

Permalink
Updated to the new Swift 2.0 syntax.
Browse files Browse the repository at this point in the history
  • Loading branch information
owensd committed Jun 19, 2015
1 parent 8b55c65 commit 327bb9c
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 57 deletions.
1 change: 1 addition & 0 deletions JSON.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@
7D7BD7B9195418CB00AC2E4E /* Project object */ = {
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0700;
LastUpgradeCheck = 0600;
ORGANIZATIONNAME = owensd.io;
TargetAttributes = {
Expand Down
2 changes: 1 addition & 1 deletion src/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public struct ErrorKeys {
public static let FilePath = "NSFilePath"
}

extension Error: Printable {
extension Error: CustomStringConvertible {
public var description: String {
return "an error..."
}
Expand Down
24 changes: 12 additions & 12 deletions src/Functional.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ infix operator ⇒ { associativity left precedence 150 }

/// Allows for a transformative function to be applied to a value, allowing for optionals.
///
/// :param: lhs The transformative function
/// :param: rhs The value to apply to the function
/// :returns: The transformation of `rhs` using `lhs`.
/// - parameter lhs: The transformative function
/// - parameter rhs: The value to apply to the function
/// - returns: The transformation of `rhs` using `lhs`.
public func <A, B>(lhs: (A -> B)?, rhs: A?) -> B? {
if let lhs = lhs {
if let rhs = rhs {
Expand All @@ -25,9 +25,9 @@ public func ⇒ <A, B>(lhs: (A -> B)?, rhs: A?) -> B? {

/// Allows for a value to be transformed by a function, allowing for optionals.
///
/// :param: lhs The value to apply to the function
/// :param: rhs The transformative function
/// :returns: The transformation of `lhs` using `rhs`.
/// - parameter lhs: The value to apply to the function
/// - parameter rhs: The transformative function
/// - returns: The transformation of `lhs` using `rhs`.
public func <A, B>(lhs: A?, rhs: (A -> B)?) -> B? {
if let lhs = lhs {
if let rhs = rhs {
Expand All @@ -40,15 +40,15 @@ public func ⇒ <A, B>(lhs: A?, rhs: (A -> B)?) -> B? {

/// Allows for a transformative function to be applied to a value.
///
/// :param: lhs The transformative function
/// :param: rhs The value to apply to the function
/// :returns: The transformation of `rhs` using `lhs`.
/// - parameter lhs: The transformative function
/// - parameter rhs: The value to apply to the function
/// - returns: The transformation of `rhs` using `lhs`.
public func <A, B>(lhs: A -> B, rhs: A) -> B { return lhs(rhs) }


/// Allows for a value to be transformed by a function.
///
/// :param: lhs The value to apply to the function
/// :param: rhs The transformative function
/// :returns: The transformation of `lhs` using `rhs`.
/// - parameter lhs: The value to apply to the function
/// - parameter rhs: The transformative function
/// - returns: The transformation of `lhs` using `rhs`.
public func <A, B>(lhs: A, rhs: A -> B) -> B { return rhs(lhs) }
14 changes: 7 additions & 7 deletions src/JSValue.Accessors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extension JSValue {

/// Attempts to retrieve a `String` out of the `JSValue`.
///
/// :returns: If the `JSValue` is a `JSString`, then the stored `String` value is returned, otherwise `nil`.
/// - returns: If the `JSValue` is a `JSString`, then the stored `String` value is returned, otherwise `nil`.
public var string: String? {
switch self.value {
case .JSString(let value): return value
Expand All @@ -24,7 +24,7 @@ extension JSValue {

/// Attempts to retrieve a `Double` out of the `JSValue`.
///
/// :returns: If the `JSValue` is a `JSNumber`, then the stored `Double` value is returned, otherwise `nil`.
/// - returns: If the `JSValue` is a `JSNumber`, then the stored `Double` value is returned, otherwise `nil`.
public var number: Double? {
switch self.value {
case .JSNumber(let value): return value
Expand All @@ -34,7 +34,7 @@ extension JSValue {

/// Attempts to retrieve a `Bool` out of the `JSValue`.
///
/// :returns: If the `JSValue` is a `JSBool`, then the stored `Double` value is returned, otherwise `nil`.
/// - returns: If the `JSValue` is a `JSBool`, then the stored `Double` value is returned, otherwise `nil`.
public var bool: Bool? {
switch self.value {
case .JSBool(let value): return value
Expand All @@ -44,7 +44,7 @@ extension JSValue {

/// Attempts to retrieve a `[String:JSValue]` out of the `JSValue`.
///
/// :returns: If the `JSValue` is a `JSObject`, then the stored `[String:JSValue]` value is returned, otherwise `nil`.
/// - returns: If the `JSValue` is a `JSObject`, then the stored `[String:JSValue]` value is returned, otherwise `nil`.
public var object: [String:JSValue]? {
switch self.value {
case .JSObject(let value): return value
Expand All @@ -54,7 +54,7 @@ extension JSValue {

/// Attempts to retrieve a `[JSValue]` out of the `JSValue`.
///
/// :returns: If the `JSValue` is a `JSArray`, then the stored `[JSValue]` value is returned, otherwise `nil`.
/// - returns: If the `JSValue` is a `JSArray`, then the stored `[JSValue]` value is returned, otherwise `nil`.
public var array: [JSValue]? {
switch self.value {
case .JSArray(let value): return value
Expand All @@ -64,7 +64,7 @@ extension JSValue {

/// Used to determine if a `nil` value is stored within `JSValue`. There is no intrinsic type for this value.
///
/// :returns: If the `JSValue` is a `JSNull`, then the `true` is returned, otherwise `false`.
/// - returns: If the `JSValue` is a `JSNull`, then the `true` is returned, otherwise `false`.
public var null: Bool {
switch self.value {
case .JSNull: return true
Expand All @@ -74,7 +74,7 @@ extension JSValue {

/// Determines if the `JSValue` has a value stored within it.
///
/// :returns: `true` if the `JSValue` has a valid value stored, `false` if the `JSValue` is `Invalid`.
/// - returns: `true` if the `JSValue` has a valid value stored, `false` if the `JSValue` is `Invalid`.
public var hasValue: Bool {
switch self.value {
case .Invalid(_): return false
Expand Down
2 changes: 1 addition & 1 deletion src/JSValue.Indexers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extension JSValue {

/// Attempts to treat the `JSValue` as a `JSObject` and perform the lookup.
///
/// :returns: A `JSValue` that represents the value found at `key`
/// - returns: A `JSValue` that represents the value found at `key`
public subscript(key: String) -> JSValue {
get {
if let dict = self.object {
Expand Down
26 changes: 13 additions & 13 deletions src/JSValue.Parsing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ extension JSValue {

/// Parses the given sequence of UTF8 code points and attempts to return a `JSValue` from it.
///
/// :param: seq The sequence of UTF8 code points.
/// - parameter seq: The sequence of UTF8 code points.
///
/// :returns: A `JSParsingResult` containing the parsed `JSValue` or error information.
/// - returns: A `JSParsingResult` containing the parsed `JSValue` or error information.
public static func parse(seq: JSParsingSequence) -> JSParsingResult {
var generator = ReplayableGenerator(seq)
let generator = ReplayableGenerator(seq)

let result = parse(generator)
if let value = result.value {
for codeunit in generator {
if codeunit.isWhitespace() { continue }
else {
var remainingText = substring(generator)
let remainingText = substring(generator)

let info = [
ErrorKeys.LocalizedDescription: ErrorCode.ParsingError.message,
Expand Down Expand Up @@ -99,7 +99,7 @@ extension JSValue {
var key = ""
var object = JSObjectType()

for (idx, codeunit) in enumerate(generator) {
for (idx, codeunit) in generator.enumerate() {
switch (idx, codeunit) {
case (0, Token.LeftCurly): continue
case (_, Token.RightCurly):
Expand Down Expand Up @@ -192,7 +192,7 @@ extension JSValue {
static func parseArray<S: SequenceType where S.Generator.Element == UInt8>(generator: ReplayableGenerator<S>) -> JSParsingResult {
var values = [JSValue]()

for (idx, codeunit) in enumerate(generator) {
for (idx, codeunit) in generator.enumerate() {
switch (idx, codeunit) {
case (0, Token.LeftBracket): continue
case (_, Token.RightBracket):
Expand Down Expand Up @@ -236,7 +236,7 @@ extension JSValue {
var exponent = 0
var exponentSign = 1

for (idx, codeunit) in enumerate(generator) {
for (idx, codeunit) in generator.enumerate() {
switch (idx, codeunit, state) {
case (0, Token.Minus, NumberParsingState.Initial):
numberSign = -1
Expand Down Expand Up @@ -300,7 +300,7 @@ extension JSValue {
}

static func parseTrue<S: SequenceType where S.Generator.Element == UInt8>(generator: ReplayableGenerator<S>) -> JSParsingResult {
for (idx, codeunit) in enumerate(generator) {
for (idx, codeunit) in generator.enumerate() {
switch (idx, codeunit) {
case (0, Token.t): continue
case (1, Token.r): continue
Expand All @@ -327,7 +327,7 @@ extension JSValue {
}

static func parseFalse<S: SequenceType where S.Generator.Element == UInt8>(generator: ReplayableGenerator<S>) -> JSParsingResult {
for (idx, codeunit) in enumerate(generator) {
for (idx, codeunit) in generator.enumerate() {
switch (idx, codeunit) {
case (0, Token.f): continue
case (1, Token.a): continue
Expand Down Expand Up @@ -355,7 +355,7 @@ extension JSValue {
}

static func parseNull<S: SequenceType where S.Generator.Element == UInt8>(generator: ReplayableGenerator<S>) -> JSParsingResult {
for (idx, codeunit) in enumerate(generator) {
for (idx, codeunit) in generator.enumerate() {
switch (idx, codeunit) {
case (0, Token.n): continue
case (1, Token.u): continue
Expand Down Expand Up @@ -396,7 +396,7 @@ extension JSValue {
static func parseString<S: SequenceType where S.Generator.Element == UInt8>(generator: ReplayableGenerator<S>, quote: UInt8) -> JSParsingResult {
var bytes = [UInt8]()

for (idx, codeunit) in enumerate(generator) {
for (idx, codeunit) in generator.enumerate() {
switch (idx, codeunit) {
case (0, quote): continue
case (_, quote):
Expand Down Expand Up @@ -535,8 +535,8 @@ extension JSValue {

static func exp(number: Double, _ exp: Int) -> Double {
return exp < 0 ?
reduce(0 ..< abs(exp), number, { x, _ in x / 10 }) :
reduce(0 ..< exp, number, { x, _ in x * 10 })
(0 ..< abs(exp)).reduce(number, combine: { x, _ in x / 10 }) :
(0 ..< exp).reduce(number, combine: { x, _ in x * 10 })
}
}

Expand Down
20 changes: 10 additions & 10 deletions src/JSValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,26 +172,26 @@ extension JSValue {
}
}

extension JSValue : Printable {
extension JSValue : CustomStringConvertible {

/// Attempts to convert the `JSValue` into its string representation.
///
/// :param: indent the indent string to use; defaults to " "
/// - parameter indent: the indent string to use; defaults to " "
///
/// :returns: A `FailableOf<T>` that will contain the `String` value if successful,
/// - returns: A `FailableOf<T>` that will contain the `String` value if successful,
/// otherwise, the `Error` information for the conversion.
public func stringify(indent: String = " ") -> String {
return prettyPrint(indent, 0)
}

/// Attempts to convert the `JSValue` into its string representation.
///
/// :param: indent the number of spaces to include.
/// - parameter indent: the number of spaces to include.
///
/// :returns: A `FailableOf<T>` that will contain the `String` value if successful,
/// - returns: A `FailableOf<T>` that will contain the `String` value if successful,
/// otherwise, the `Error` information for the conversion.
public func stringify(indent: Int) -> String {
let padding = reduce(0..<indent, "") { s, i in return s + " " }
let padding = (0..<indent).reduce("") { s, i in return s + " " }
return prettyPrint(padding, 0)
}

Expand All @@ -203,7 +203,7 @@ extension JSValue : Printable {

/// Used to compare two `JSValue` values.
///
/// :returns: `True` when `hasValue` is `true` and the underlying values are the same; `false` otherwise.
/// - returns: `True` when `hasValue` is `true` and the underlying values are the same; `false` otherwise.
public func ==(lhs: JSValue, rhs: JSValue) -> Bool {
switch (lhs.value, rhs.value) {
case (.JSNull, .JSNull):
Expand Down Expand Up @@ -231,7 +231,7 @@ public func ==(lhs: JSValue, rhs: JSValue) -> Bool {

extension JSValue {
func prettyPrint(indent: String, _ level: Int) -> String {
let currentIndent = indent == "" ? "" : join(indent, map(0...level, { (item: Int) in "" }))
let currentIndent = indent == "" ? "" : indent.join((0...level).map({ (item: Int) in "" }))
let nextIndent = currentIndent + indent

let newline = indent == "" ? "" : "\n"
Expand All @@ -249,10 +249,10 @@ extension JSValue {
return "\"\(escaped)\""

case .JSArray(let array):
return "[\(newline)" + join(",\(newline)", array.map({ "\(nextIndent)\($0.prettyPrint(indent, level + 1))" })) + "\(newline)\(currentIndent)]"
return "[\(newline)" + ",\(newline)".join(array.map({ "\(nextIndent)\($0.prettyPrint(indent, level + 1))" })) + "\(newline)\(currentIndent)]"

case .JSObject(let dict):
return "{\(newline)" + join(",\(newline)", map(dict, { "\(nextIndent)\"\($0)\":\(space)\($1.prettyPrint(indent, level + 1))"})) + "\(newline)\(currentIndent)}"
return "{\(newline)" + ",\(newline)".join(dict.map({ "\(nextIndent)\"\($0)\":\(space)\($1.prettyPrint(indent, level + 1))"})) + "\(newline)\(currentIndent)}"

case .JSNull:
return "null"
Expand Down
2 changes: 1 addition & 1 deletion src/ReplayableGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public final class ReplayableGenerator<S: SequenceType> : GeneratorType, Sequenc

/// Initializes a new `ReplayableGenerator<S>` with an underlying `SequenceType`.
///
/// :param: sequence the sequence that will be used to traverse the content.
/// - parameter sequence: the sequence that will be used to traverse the content.
public init(_ sequence: S) {
self.generator = sequence.generate()
}
Expand Down
Loading

0 comments on commit 327bb9c

Please sign in to comment.