How to use custom formatters in json2csv - json2csv

I've read through the documentation on how to use the built in formatters but Im facing some issues..
Here's my code:
import { Parser, stringFormatter } from "json2csv";
const jsonConvertToCsv = (jsonData: any) => {
const csvOpts = {
formatters: {
string: stringFormatter()
},
}
const json2csvParser = new Parser(csvOpts);
return json2csvParser.parse(jsonData);
}
But somehow it doesnt seem to be able to instantiate the in built formatter that I imported. I get the error that the stringFormatter is not a function but the documentation shows that it can be imported??
https://github.com/zemirco/json2csv

Related

CoreStore Xcode data fault issue when fetching objects

I'm new to CoreStore and attempting to test my ability to create, store, and fetch CoreStore Objects correctly. It appears entries are being created based on results of dataStack.fetchCount() and transaction.create(), but the data is unavailable or not stored correctly based on my results of dataStack.fetchAll() (it says data: fault at the end of each object) and inspection of the SQLite database with Liya software reveals no entries in the table. I'm using CoreStore v8.0.1 and Xcode v12.4. My example code (using synchronous execution) is below. I appreciate your input into troubleshooting the issue. Thanks.
MyImage.swift:
import CoreStore
final class MyImage: CoreStoreObject {
#Field.Stored("desc")
var desc: String = ""
}
MyApp.swift
import SwiftUI
import CoreStore
import Foundation
#main
struct MyApp: App {
static let dataStack: DataStack = {
let dataStack = DataStack(
CoreStoreSchema(
modelVersion: "V1",
entities: [
Entity<MyImage>("MyImage")
],
versionLock: [
"MyImage": [..., ..., ..., ...]
]
)
)
try! dataStack.addStorageAndWait(
SQLiteStore( fileName: "MyStore.sqlite",
//localStorageOptions: .allowSynchronousLightweightMigration,
localStorageOptions: .recreateStoreOnModelMismatch
)
)
print(dataStack.coreStoreDumpString)
return dataStack
}()
init() {
CoreStoreDefaults.dataStack = MyApp.dataStack
try! dataStack.perform(
synchronous: { (transaction) in
let test_obj = transaction.create(Into<MyImage>())
test_obj.desc = "Description"
},
waitForAllObservers: true
)
do {
let objects = try dataStack.fetchAll(From<MyImage>())
print(objects)
} catch { print("error fetching")}
}
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
I appreciate your help.
It turns out that the fetch results will display data:fault until the properties of the objects are accessed. The following example code will display the value of the properties:
let objects = try dataStack.fetchAll(From<MyImage>())
print(objects.map { $0.desc })

How do you access a KrollCallback in swift?

I have implemented a custom framework and it is integrated into a hyperloop enabled project.
I am passing function definitions as arguments to a swift function that takes a protocol.
Javascript:
var customListener = {};
customListener.onPayEvent = function(event) {
console.log("moop");
};
var PayView = require('Pay/PayView');
var payView = PayView.alloc().initWithPayEventListener(customListener);
This javascript function definition comes in as a KrollCallback.
Swift Code:
class PayListener: NSObject, WootPayEventListener {
let payEventListener: PayEventListener
init(payEventListener: PayEventListener) {
self.payEventListener = payEventListener
}
public func onPayEvent(PayEvent: PayEvent) {
os_log("calling payEventListener.onPayEvent")
os_log("listener description = %{public}#", self.payEventListener.description)
os_log("listener debugDescription = %{public}#", self.payEventListener.debugDescription ?? "")
// self.payEventListener.onPayEvent(payEvent: "woo dogggy")
}
}
How do I call methods on this object so that I can return the result from swift back to javascript?
I was able to do this by building the TitaniumKit framework locally and then importing it into my project.
The TitaniumKit source code is here: https://github.com/appcelerator/titanium_mobile/tree/master/iphone/TitaniumKit
The current build steps for the framework are below
carthage build --archive
Once I imported it into the project I was able to use KrollCallback like this:
class SimplePayListener: NSObject, SimplePayEventListener {
let payEventListener: PayEventListener
init(payEventListener: PayEventListener) {
self.payEventListener = payEventListener
}
public func onPayEvent(payEvent_ payEvent: String) {
os_log("SimplePayListener event description = %{public}#", fivestarsPayEvent.description)
let appceleratorCallback:KrollCallback = self.payEventListener as! KrollCallback
appceleratorCallback.call([payEvent], thisObject: self.payEventListener)
}
}

Generic parameter 'Value' could not be inferred

I am trying to make a Data-Storage using NSCoder, for some weird reason, its showing this error to me where i try to use the .encode keyword, please help me understand what i'm doing wrong..
let encoder = PropertyListEncoder()
do {
let data = try encoder.encode(self.itemArray) // <--- showing error here
} catch {
}
Never-mind, I found the problem! If you guys are facing the same problem where you make your array takes data specified in a class, you need to make the class 'Encodable' ie
import Foundation
class CellItemReg : Encodable { // <-- 'Encodable'
var done : Bool = false
var title : String = ""
}
This fixed for me in Swift iOS.
Inherit Codable in the class which you are trying to encode.
In your case,
let encoder = PropertyListEncoder()
do {
let data = try encoder.encode(self.itemArray) // <--- showing error here
} catch {
}
Let's assume itemArray is the array of a class named 'Item'. Then your 'Item' needs to inherit Codable in swift.
Just as below.
import Foundation
class Item: Codable {
var id: Int!
}
All the best!

"Argument labels '(options:)' do not match any available overloads" occurs in PagingMenuController

I use PagingMenuController 2.0.0 in my project using swift3.
I have a problem when I write following codes.
These codes are copied from README of PagingMenuController.
An error "Argument labels '(options:)' do not match any available overloads" occurs in the last line.
struct MenuItem1: MenuItemViewCustomizable {}
struct MenuItem2: MenuItemViewCustomizable {}
struct MenuOptions: MenuViewCustomizable {
var itemsOptions: [MenuItemViewCustomizable] {
return [MenuItem1(), MenuItem2()]
}
}
struct PagingMenuOptions: PagingMenuControllerCustomizable {
var componentType: ComponentType {
return .all(menuOptions: MenuOptions(), pagingControllers: [UIViewController(), UIViewController()])
}
}
let options = PagingMenuOptions()
let pagingMenuController = PagingMenuController(options: options)

Swift 2 SQLite.swift module reference

Since I have updated to Xcode 7 and swift 2, I'm getting this errors:
No type named 'Query' in module 'SQLite'
Use of undeclared type 'Database'
using this code:
let table:SQLite.Query
init(db:Database){
table = db["BucketType"]
}
I'm using the swift2 branch of SQLite.swift, but it looks like my project, it can't find the reference SQLite.swift module.
Also have import SQLite on every file I use SQLite.swift with.
I've tried the manual integration and the cocoa pods, but with the same results.
It was working with Xcode 6.4.
I have something like this...
class DBHelper {
static let instance = DBHelper() // singleton
var db : Connection
init() {
do {
self.db = try Connection("\(Util.getDBPath())/db.sqlite3")
createTablesIfNotExists()
} catch {
Logger.error("Could not create a connection to database")
}
}
func createTablesIfNotExists() {
// Logs
let logs = Table(TLog.TABLE_NAME) // just the name of your table
do {
try db.run(logs.create(ifNotExists: true) { t in
t.column(TLog.ID, primaryKey: true) // Expression<Int>("id")
t.column(TLog.TS) // Expression<String>("ts")
t.column(TLog.TAG) // Expression<String>("tag")
t.column(TLog.TYPE) ...
t.column(TLog.CONTENT) ...
})
} catch {
Logger.error("Could not create table Logs")
}
}
And.. Util.getDBPath would be...
import SystemConfiguration
class Util {
class func getDBPath() -> String {
let path = NSSearchPathForDirectoriesInDomains(
.DocumentDirectory, .UserDomainMask, true
).first
return path!
}
}
Hope this help you.

Resources