Skip to content

Instantly share code, notes, and snippets.

@lmzach09
lmzach09 / convert-object-to-json-string.js
Last active July 27, 2019 04:15
Object To String Fail with toString
let obj = { "key": "value" };
let myString = obj.toString();
console.log(myString);
// '[object Object]'
@lmzach09
lmzach09 / convert-object-to-json-string.js
Created July 27, 2019 04:16
Object to JSON string Success in JavaScript
let obj = { "key": "value" };
let myString = JSON.stringify(obj);
console.log(myString);
// '{"key":"value"}'
@lmzach09
lmzach09 / json-stringify-formatted-spaces.js
Last active July 27, 2019 19:00
Stringify a JS object to JSON string with 4 spaces formatting
let obj = { "key": "value" };
let myString = JSON.stringify(obj, null, 4); // 4 space indentations
console.log(myString);
// {
// "key": "value"
// }
@lmzach09
lmzach09 / json-stringify-formatted-tabs.js
Created July 27, 2019 04:21
Stringify a JS object to JSON string with tab formatting
let obj = { "key": "value" };
let myString = JSON.stringify(obj, null, '\t'); // tab
console.log(myString);
// {
// "key": "value"
// }
@lmzach09
lmzach09 / json-parse-example.js
Last active July 27, 2019 04:23
Parsing a JSON string to JavaScript object example
let str = '{ "a": 123 }';
let obj = JSON.parse(str);
console.log(obj);
// { a: 123 }
@lmzach09
lmzach09 / isolate-import-example.dart
Created August 5, 2019 05:08
Imports needed for Dart/Flutter Isolates, which is asynchronous code that runs in a separate thread.
import 'dart:async';
import 'dart:isolate';
@lmzach09
lmzach09 / isolate-initializer.dart
Created August 5, 2019 05:12
An init function for creating a Dart/Flutter Isolate. It returns the SendPort so the main thread can send messages to the Isolate. It also listens for messages passed from the Isolate.
Future<SendPort> initIsolate() async {
Completer completer = new Completer<SendPort>();
ReceivePort isolateToMainStream = ReceivePort();
isolateToMainStream.listen((data) {
if (data is SendPort) {
SendPort mainToIsolateStream = data;
completer.complete(mainToIsolateStream);
} else {
print('[isolateToMainStream] $data');
@lmzach09
lmzach09 / isolate-function.dart
Last active August 6, 2019 02:03
This is a function that a Dart Isolate can execute. It will listen for messages from the main thread and also send messages to the main thread.
void myIsolate(SendPort isolateToMainStream) {
ReceivePort mainToIsolateStream = ReceivePort();
isolateToMainStream.send(mainToIsolateStream.sendPort);
mainToIsolateStream.listen((data) {
print('[mainToIsolateStream] $data');
});
isolateToMainStream.send('This is from myIsolate()');
}
@lmzach09
lmzach09 / dart-main-function.dart
Created August 6, 2019 02:08
This Dart main function kicks off an async function to set up a second thread using the Isolate class.
void main() async {
SendPort mainToIsolateStream = await initIsolate();
mainToIsolateStream.send('This is from main()');
}
@lmzach09
lmzach09 / isolate-example.dart
Created August 6, 2019 02:13
Dart/Flutter Isolate example that has two-way message passing ability.
// Example of bi-directional communication between a main thread and isolate.
import 'dart:io'; // for exit();
import 'dart:async';
import 'dart:isolate';
Future<SendPort> initIsolate() async {
Completer completer = new Completer<SendPort>();
ReceivePort isolateToMainStream = ReceivePort();