Skip to content

Instantly share code, notes, and snippets.

View yongjun21's full-sized avatar

Yong Jun yongjun21

  • Singapore
  • 17:42 (UTC +08:00)
View GitHub Profile
@yongjun21
yongjun21 / euclidean.ipynb
Last active September 24, 2020 11:52
Neat little trick to efficiently compute Euclidean distance in pandas
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@yongjun21
yongjun21 / Promise.filter.js
Last active September 4, 2024 06:28
Complete implementation of Promise.filter
Promise.filter = function (iterable, filterer, options = {}) {
let concurrency = options.concurrency || Infinity
let index = 0
const results = []
const predicates = []
const pending = []
const iterator = iterable[Symbol.iterator]()
while (concurrency-- > 0) {
@yongjun21
yongjun21 / Promise.map.js
Last active September 4, 2024 06:28
Complete implementation of Promise.map
Promise.map = function (iterable, mapper, options = {}) {
let concurrency = options.concurrency || Infinity
let index = 0
const results = []
const pending = []
const iterator = iterable[Symbol.iterator]()
while (concurrency-- > 0) {
const thread = wrappedMapper()
@yongjun21
yongjun21 / delay.js
Last active May 29, 2023 02:59
Alternative implementation of timed delay decorators
function delay (invoke, ms) {
return (...args) => new Promise(resolve => {
setTimeout(resolve, ms)
}).then(() => invoke.apply(...args))
}
function delay2 (invoke, ms) {
return (...args) => new Promise(resolve => {
setTimeout(resolve, ms, invoke(...args))
})
@yongjun21
yongjun21 / Promise.waterfall.js
Last active May 29, 2023 03:00
Simple Promise.waterfall implementation
Promise.waterfall = function (array, invoke) {
let pending = Promise.resolve()
const results = []
for (const item of array) {
pending = pending
.then(() => invoke(item, i))
.then(result => results.push(result))
}
@yongjun21
yongjun21 / Tooltip.vue
Created September 25, 2019 04:01
Tooltip that tracks position of a target element using mutation observer
<template>
<div class="tooltip" :style="style">
<slot></slot>
</div>
</template>
<script>
export default {
props: {
target: {
@yongjun21
yongjun21 / readLineByLine.js
Last active July 21, 2020 12:14
Helper function to read file line by line with small memory footprint. Returns a thenable that can be chained with `.filter` and `.map` methods
const readline = require('readline')
const fs = require('fs')
module.exports = function (...args) {
const transforms = []
let state = 'pending'
let error = null
const rl = readline.createInterface({
input: fs.createReadStream(...args)
@yongjun21
yongjun21 / ObjectFitVideo.vue
Created May 28, 2019 09:04
Vue component to polyfill object-fit CSS property on videos
<template>
<video class="object-fit-video" v-bind="$attrs" v-on="$listeners" :style="videoStyle">
<slot></slot>
</video>
</template>
<script>
const supportsObjectFit = window.CSS && window.CSS.supports &&
window.CSS.supports('object-fit', 'cover') &&
!/Edge/.test(window.navigator.userAgent)
@yongjun21
yongjun21 / Promise.js
Last active May 17, 2019 07:37
Light weight Promise polyfill
const Promise = window.Promise || (function () {
function PromiseLike (init) {
let state = 'pending'
let resolved, error
const pendingResolve = []
const pendingReject = []
function resolve (value) {
state = 'resolved'
resolved = value
pendingResolve.forEach(function (fn) { fn(value) })
@yongjun21
yongjun21 / makeAnimated.js
Last active May 10, 2019 04:49
Turn any Vue component into an animated component
import TweenLite from 'gsap/TweenLite'
import {_ANIMATE_, currentAnimations, defaultConfig} from './shared'
export default function (Target, animatedProps = []) {
if (animatedProps.length === 0) return Target
const props = {
animation: Object
}
animatedProps.forEach(prop => { props[prop] = null })