Skip to content

Instantly share code, notes, and snippets.

@skyjur
skyjur / component.ts
Created April 16, 2018 07:03
Simple state preserving in fuse-box HMR (hot module reload)
import {hot} from '.hmr';
class MyComp extends React.Component {
componentWillMount() {
hot(this)
}
}
@skyjur
skyjur / utils.dart
Created January 22, 2019 06:01
dart:randomString
import 'dart:math';
String randomString(int length) {
final rand = Random.secure();
final codeUnits = List.generate(length, (index) {
return rand.nextInt(33) + 89;
});
return String.fromCharCodes(codeUnits);
}
@skyjur
skyjur / main.dart
Last active January 22, 2019 14:36
void main() {
Map firstMap = { "id": 1, "value": [{"key": "A"}, {"key": "B"}]};
List valueList = firstMap["value"];
List<Map> newList = valueList.map((valueItem) => {
"id": firstMap["id"],
"value": [valueItem]
}).toList();
main() async {
List<String> testList = ['sfs','sdfsdf', 'sfdsf'];
for(final value in testList) {
await Future.delayed(Duration(seconds: 1));
print('Writing another word $value');
};
}
@skyjur
skyjur / bitslice.js
Created February 5, 2019 06:28
bit slicing
// Binary bit slice from the right to the left
// msb: n-th more significant bit from the right (inclusive)
// lsb: n-th less signnificant bit from the right (inclusive)
//
// bitSlice(x, 2, 0) - last 3 bits of a number
// bitSlice(x, 3, 2) - 4th and 3rd least significant bits
function bitSlice(n, msb, lsb) {
return (n & ((1 << (msb + 1)) - 1)) >> lsb
}
@skyjur
skyjur / main.dart
Created July 25, 2019 10:06
Folding maps
void main() {
final map = {
Product(price: 1): 2,
Product(price: 2): 3,
};
final total = map.entries.fold(
0,
(total, entry) => total + (entry.key.price * entry.value)
);
@skyjur
skyjur / main.dart
Created July 26, 2019 02:27
updating final values
void main() {
final order1 = Order(
address: Address(
street: "Daguan",
city: "Taipei",
postcode: "231",
country: "Taiwan"
),
person: Person(
firstName: "Chloe",
@skyjur
skyjur / lazy_wrapper.js
Last active January 27, 2020 02:34
javascript string literal tag for pg queries
const { query } = require("../connection")
/**
* @template T
* @param {Pg.QueryConfig | string} queryConfig
* @param {Ork.Context} ctx
* @returns {Sql.LazyQuery<T>}
*/
function lazyQuery(queryConfig, ctx = {}) {
let { name = "", text, values = [] } =
@skyjur
skyjur / run_changefeed_forever.js
Created January 30, 2020 21:58
Change feed listener
const _log = require("../../utils/logger")
/**
* @typedef {{
* query: any,
* includeInitial?: boolean,
* onChange(oldVal: any, newVal: any, ctx?: Ork.Context): any,
* onState?: (state: string, ctx?: Ork.Context) => any,
* dataTimeoutInterval?: number,
* stateTimeoutInterval?: number,
@skyjur
skyjur / api.apib
Created June 11, 2020 11:44
Orkestro API
FORMAT: 1A
HOST: https://api.orkestro.io/
# Orkestro API
Orkestro's public API enables customers to programatically integrate Orkestro's marketplace capabilities into their own platform.
API consumers are able to submit orders, get their statuses, cancel them and get constant status updates for them through webhooks.
## Glossary
- **Order** - Represents a logical unit of work to get a parcel from location A to B using Orkestro's Platform.