Skip to content

Instantly share code, notes, and snippets.

Avatar
🦄
Is this the new MSN Messenger

Lim Chee Aun cheeaun

🦄
Is this the new MSN Messenger
View GitHub Profile
@cheeaun
cheeaun / median.js
Created Jun 14, 2020
Find median from array of numbers (mutable, due to .sort())
View median.js
function median(numbers) {
const middle = (numbers.length + 1) / 2;
const sorted = numbers.sort((a, b) => a - b);
const isEven = sorted.length % 2 === 0;
return isEven
? (sorted[middle - 1.5] + sorted[middle - 0.5]) / 2
: sorted[middle - 1];
}
@cheeaun
cheeaun / p-timeout.js
Created Apr 25, 2020
promisified setTimeout
View p-timeout.js
const pTimeout = (t) =>
new Promise((res, rej) => {
setTimeout(res, t);
});
@cheeaun
cheeaun / ImperativeText.js
Created Apr 24, 2020
React Native's Text component, but imperative instead for setting the text value
View ImperativeText.js
import React, { forwardRef, useState, useImperativeHandle } from 'react';
import { Text } from 'react-native';
export default forwardRef((props, ref) => {
const [text, setText] = useState('');
useImperativeHandle(ref, () => ({
setText: t => {
if (t) setText(t);
},
}));
@cheeaun
cheeaun / resize-nearest-neighbour.js
Created Mar 21, 2020
Resize (2D but flat) array of values with nearest neighbour algorithm
View resize-nearest-neighbour.js
const width = 300;
const height = 200;
const newWidth = width * 2;
const newHeight = height * 2;
const xRatio = width / newWidth;
const yRatio = height / newHeight;
const resizeValues = (id, values) => {
const newValues = new Array(newWidth * newHeight);
for (let i = 0; i < newHeight; i++) {
for (let j = 0; j < newWidth; j++) {
@cheeaun
cheeaun / durian.geojson
Last active May 1, 2019
Durio species GeoJSON, extracted from https://github.com/cheeaun/exploretrees-sg dataset
View durian.geojson
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@cheeaun
cheeaun / singapore-boundary.geojson
Created Feb 12, 2019
Singapore land boundary GeoJSON (hi-res)
View singapore-boundary.geojson
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@cheeaun
cheeaun / checkweathersg.sh
Last active Nov 18, 2018
One-liner command to grab rain radar snapshot in ASCII format from https://checkweather.sg
View checkweathersg.sh
node -e "require('https').get('https://api.checkweather.sg/now?ascii=1', res => res.on('data', d => process.stdout.write(d + '\n\n')))"
@cheeaun
cheeaun / .block
Last active Jan 28, 2018
Film Flowers, Full Starter Code
View .block
license: gpl-3.0
@cheeaun
cheeaun / .block
Created Jan 26, 2018
Film Flowers, Single Starter Code
View .block
license: gpl-3.0
You can’t perform that action at this time.