Swiftpack.co - Package - 0xLeif/Object

Object (SwiftUI-Object)

Basic Example

let obj = Object()
obj.variables["qwerty"] = 12456
obj.functions["printy"] = { input in
    "{ \(Object(input).value() ?? -1) }"
}

XCTAssertEqual(obj.run(function: "printy", value: obj.qwerty).value(), "{ 12456 }")
XCTAssertEqual(obj.run(function: "printy", value: obj.variable("qwerty")).value(), "{ 12456 }")
XCTAssertEqual(obj.run(function: "printy", withInteralValueName: "qwerty").value(), "{ 12456 }" )

Data Example

var mockDataView = UIView.later { later in
    // Fetch Data
    later.fetch(url: URL(string: "https://jsonplaceholder.typicode.com/todos/3")!)
        // Store Values in an Object
        .map { (data, response, error) in
            Object(data).configure {
                $0.add(variable: "response", value: response as Any)
                $0.add(variable: "error", value: error as Any)
            }
    }
        // Save Data
        .flatMap { object in
            object.value(decodedAs: MockData.self)
                .save(withKey: "mock_03")
    }
        // Load Data
        .flatMap { _ in
            MockData.load(withKey: "mock_03")
    }
        // Present UI
        .flatMap { data in
            later.main {
                Label("Data: \(data.title)").number(ofLines: 100)
            }
    }
}

Github

link
Stars: 1

Dependencies

Used By

Total: 0

Releases

print(Object(data)) - 2020-09-10 01:52:18

Example Print

// https://jsonplaceholder.typicode.com/users/7
/*
{
  "id": 7,
  "name": "Kurtis Weissnat",
  "username": "Elwyn.Skiles",
  "email": "Telly.Hoeger@billy.biz",
  "address": {
    "street": "Rex Trail",
    "suite": "Suite 280",
    "city": "Howemouth",
    "zipcode": "58804-1099",
    "geo": {
      "lat": "24.8918",
      "lng": "21.8984"
    }
  },
  "phone": "210.067.6132",
  "website": "elvis.io",
  "company": {
    "name": "Johns Group",
    "catchPhrase": "Configurable multimedia task-force",
    "bs": "generate enterprise e-tailers"
  }
}
*/
Later.fetch(url: URL(string: "https://jsonplaceholder.typicode.com/users/7")!)
    .whenSuccess { (data, response, error) in
        print(Object(data))
}

print(Object(data))

/*
Object {
|    Variables
|    * email: Telly.Hoeger@billy.biz (__NSCFString)
|    * _json: {
  "id": 7,
  "name": "Kurtis Weissnat",
  "username": "Elwyn.Skiles",
  "email": "Telly.Hoeger@billy.biz",
  "address": {
    "street": "Rex Trail",
    "suite": "Suite 280",
    "city": "Howemouth",
    "zipcode": "58804-1099",
    "geo": {
      "lat": "24.8918",
      "lng": "21.8984"
    }
  },
  "phone": "210.067.6132",
  "website": "elvis.io",
  "company": {
    "name": "Johns Group",
    "catchPhrase": "Configurable multimedia task-force",
    "bs": "generate enterprise e-tailers"
  }
} (String)
|    * _value: 500 bytes (Data)
|    * username: Elwyn.Skiles (__NSCFString)
|    * address: {
    city = Howemouth;
    geo =     {
        lat = "24.8918";
        lng = "21.8984";
    };
    street = "Rex Trail";
    suite = "Suite 280";
    zipcode = "58804-1099";
} (__NSDictionaryI)
|    * website: elvis.io (NSTaggedPointerString)
|    * phone: 210.067.6132 (__NSCFString)
|    * id: 7 (__NSCFNumber)
|    * name: Kurtis Weissnat (__NSCFString)
|    * company: {
    bs = "generate enterprise e-tailers";
    catchPhrase = "Configurable multimedia task-force";
    name = "Johns Group";
} (__NSDictionaryI)
}
*/

print(Object(data).address)

/*
Object {
|    Variables
|    * city: Howemouth (NSTaggedPointerString)
|    * zipcode: 58804-1099 (__NSCFString)
|    * suite: Suite 280 (NSTaggedPointerString)
|    * street: Rex Trail (NSTaggedPointerString)
|    * geo: {
    lat = "24.8918";
    lng = "21.8984";
} (__NSDictionaryI)
}
*/

print(Object(data).address.geo)

/*
Object {
|    Variables
|    * lat: 24.8918 (NSTaggedPointerString)
|    * lng: 21.8984 (NSTaggedPointerString)
}
*/

Object(Any) { /* Configure */ } - 2020-09-09 15:58:05

Object("Init Value") {
    $0.add(variable: "otherValue", value: "Different Value")
}

value(decodedAs:) - 2020-09-05 17:42:46

Example Use

var mockDataView = UIView.later { later in
    // Fetch Data
    later.fetch(url: URL(string: "https://jsonplaceholder.typicode.com/todos/3")!)
        // Store Values in an Object
        .map { (data, response, error) in
            Object(data).configure {
                $0.add(variable: "response", value: response as Any)
                $0.add(variable: "error", value: error as Any)
            }
    }
        // Save Data
        .flatMap { object in
            object.value(decodedAs: MockData.self)
                .save(withKey: "mock_03")
    }
        // Load Data
        .flatMap { _ in
            MockData.load(withKey: "mock_03")
    }
        // Present UI
        .flatMap { data in
            later.main {
                Label("Data: \(data.title)").number(ofLines: 100)
            }
    }
}

Object(Any).add() - 2020-09-05 16:05:59

struct SMObj: Codable {
    let id = 1
    let string = "Object"
}

let smObj = SMObj().object

let dictObj = Object([
    "some": 3.14,
    3.14: "some"
])

let arrayObj = Object((1 ... 100).map { $0 })

let empty = Object()
let obj = Object(
    ["id": 10]
)

[smObj, dictObj]
    .forEach {
        empty.consume($0)
}
obj.add(childObject: empty)
obj.add(array: arrayObj.array)

Object(Any).subscript - 2020-09-05 01:51:30

Data Example

Later.fetch(url: URL(string: "https://jsonplaceholder.typicode.com/users/7")!)
    .whenSuccess { (data, response, error) in
        let obj = Object(data)
        
        XCTAssertEqual(obj.address.street.value(), "Rex Trail")
        XCTAssertEqual(obj.address.geo.lng.value(), "21.8984")
        XCTAssertEqual(obj.address.geo.lat.value(), "24.8918")
}

Object(Any) - 2020-09-05 01:25:48

Example Code

Object()

let obj = Object()
obj.variables["qwerty"] = 12456
obj.functions["printy"] = { input in
    "{ \(Object(input).value() ?? -1) }"
}

XCTAssertEqual(obj.runFunction(named: "printy", value: obj.qwerty).value(), "{ 12456 }")
XCTAssertEqual(obj.runFunction(named: "printy", value: obj.variable("qwerty")).value(), "{ 12456 }")
XCTAssertEqual(obj.runFunction(named: "printy", withInteralValueName: "qwerty").value(), "{ 12456 }" )

Object(Object(...))

let obj = Object()
obj.variables["qwerty"] = 123456
obj.functions["toString"] = { "\($0 ?? "-1")" }
obj.functions["printy"] = { input in
    guard let input = input as? Int else {
        throw ObjectError.invalidParameter
    }
    return "{ \(input) }"
}

let newObj = Object(obj)

XCTAssertEqual(newObj.qwerty.value(), 123456)
XCTAssertEqual(newObj.runFunction(named: "printy", value: 654321).value(), "{ 654321 }")
XCTAssertEqual(newObj.runFunction(named: "toString", value: true).value(), "true")

Object([Int])

let obj = Object((1 ... 100).map { $0 })

XCTAssertEqual(obj.array.count, 100)

Object([AnyHashable: Any])

func testDictionary() {
let obj = Object([
    "some": 3.14,
    3.14: "some"
])

XCTAssertEqual(obj.some.value(), 3.14)
XCTAssertEqual(obj.variable(3.14).value(), "some")

Codable.object

struct SMObj: Codable {
    let id = 1
    let string = "Object"
}

let obj = SMObj().object

XCTAssertEqual(obj.id.value(), 1)
XCTAssertEqual(obj.string.value(), "Object")

Object(Data)

Later.fetch(url: URL(string: "https://jsonplaceholder.typicode.com/posts/1")!)
    .whenSuccess { (data, response, error) in
        let obj = Object(data)
        
        XCTAssertEqual(obj.userId.value(), 1)
        XCTAssertEqual(obj.id.value(), 1)
        XCTAssertEqual(obj.title.value(), "sunt aut facere repellat provident occaecati excepturi optio reprehenderit")
        XCTAssertEqual(obj.body.value(), "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto")
}

Object([Data])

Later.fetch(url: URL(string: "https://jsonplaceholder.typicode.com/posts")!)
    .whenSuccess { (data, response, error) in
        let obj = Object(data)
        
        XCTAssertEqual(obj.array.count, 100)
        
        let first: Object? = obj.array.first
        
        XCTAssertEqual(first?.userId.value(), 1)
        XCTAssertEqual(first?.id.value(), 1)
        XCTAssertEqual(first?.title.value(), "sunt aut facere repellat provident occaecati excepturi optio reprehenderit")
        XCTAssertEqual(first?.body.value(), "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto")
}

Init Object - 2020-09-04 14:39:27

let obj = Object()
obj.variables["qwerty"] = 12456
obj.functions["printy"] = { input in
    guard let input = input as? Int else {
        throw ObjectError.invalidParameter
    }
    print("[[{ \(input) }]]")
    return "{ \(input) }"
}

obj.runFunction(named: "printy", value: obj.qwerty)
obj.runFunction(named: "printy", value: obj.variable("qwerty"))
obj.runFunction(named: "printy", withInteralValueName: "qwerty")