Skip to content

Instantly share code, notes, and snippets.

@thekhairul
thekhairul / vueBackgroundImgCall.scss
Created June 6, 2018 09:17
background img url from scss in vue
div {
background : url(../assets/images/); // don't use quotes in url like url('')
// and use paths relative to the file editing
}
@thekhairul
thekhairul / gist:39bbc47b6845085feac7c91f65fb4e5b
Created November 28, 2018 02:04 — forked from CristinaSolana/gist:1885435
Keeping a fork up to date

1. Clone your fork:

git clone git@github.com:YOUR-USERNAME/YOUR-FORKED-REPO.git

2. Add remote from original repository in your forked repository:

cd into/cloned/fork-repo
git remote add upstream git://github.com/ORIGINAL-DEV-USERNAME/REPO-YOU-FORKED-FROM.git
git fetch upstream
@thekhairul
thekhairul / index.js
Created March 11, 2021 03:07
Vue $watch only once
// Reference: https://stackblitz.com/edit/vue-watch-only-once?file=index.js
import Vue from 'vue';
// Watch "isLive" via $watch to invoke the returned unwatch
// handler after the component went live for the first time.
// Because the websocket stays live and only die visualization
// is stopped.
const Child = Vue.component('child', {
props: {
@thekhairul
thekhairul / Component.js
Last active September 17, 2021 17:00
Modify React children while rendering
// modify react childrens while rendering in class component (can also be done in FCs)
render() {
if (this.props.children) {
return React.Children.map(this.props.children, (childElement) => {
if (childElement.type === 'string') return childElement; // return non React components as it is
return React.cloneElement(childElement, {
// pass additional props implicitly to the child react components
prop1: this.state.prop1,
prop2: this.prop2
})
@thekhairul
thekhairul / canvas.js
Last active September 9, 2021 16:13
Download image from browser canvas API
canvas.toBlob((blob) => {
const anchor = document.createElement('a');
anchor.download = 'my-file-name.jpeg'; // optional, but you can give the file a name
anchor.href = URL.createObjectURL(blob);
anchor.click(); // ✨ magic!
URL.revokeObjectURL(anchor.href); // remove it from memory and save on memory!
}, 'image/jpeg', 0.9);
@thekhairul
thekhairul / README.md
Last active August 31, 2022 16:25
Gotchas

css 'fixed' position not relative to viewport but to parent container

  • normally we expect 'fixed' position to be relative to the browser viewport. but for certain curcumstance that might not be the case. when a 'fixed' positioned element has a parent with 'transform' rule applied it becomes relative to that parent

Github pages

  • github pages require a .nojekyll file to be present to properly work with non jekyll project like nuxt

Jest 'Unexpected token <' error for vue components

  • To convert vue components Jest needs vue-jest installed and some extra jest config
@thekhairul
thekhairul / removeDuplicates.py
Created April 3, 2022 04:31
Leetcode remove duplicates from sorted array with python3
def removeDuplicates(nums):
nums[:] = [x for idx, x in enumerate(nums) if idx == 0 or x != nums[idx - 1]]
return len(nums)
@thekhairul
thekhairul / maxp3.py
Last active April 5, 2022 04:59
Interviewbit - Return the highest product possible by multiplying 3 numbers from the array - python
def maxp3(self, A):
A.sort()
maxp3 = 1
max3 = A[-3:]
for x in max3:
maxp3 *= x
edge = A[0] * A[1] * A[-1]
return max(edge, maxp3)
@thekhairul
thekhairul / maxDistance.py
Created April 8, 2022 05:41
InterviewBit - max distance between indices of an integer array (Python)
def maximumGap(self, A):
# step 0: make a list of touples (value, index) and sort it by value (O(nlogn))
sortedA = sorted(((v, i) for i, v in enumerate(A)))
# step 1: init maxGap to store maximum gap between 2 indices
# and minNum to store the minimum index so far in the loop
maxGap = 0
minNum = sortedA[0][1]
#step 2: loop through sortedA to find maximum index gap (O(n))
for i in range(1,len(sortedA)):
if sortedA[i][1] < minNum:
@thekhairul
thekhairul / hotelBooking.py
Created April 8, 2022 10:52
InterviewBit - hotel booking possible or not with python
def hotel(self, arrive, depart, K):
arrive.sort()
depart.sort()
n = len(arrive)
i,j = 0,0
booked = 0
while i < n and j < n:
if arrive[i] < depart[j]: