Skip to content

Instantly share code, notes, and snippets.

@GingerBear
GingerBear / url_param.js
Created January 14, 2014 22:47
JavaScript URL parameters parsing by Regular Expression
function url_param(url) {
var _url = url || window.location.href;
var array = _url.match(/\w+=\w+/g);
var result = {};
var tmp = [];
for (var i = 0; i < array.length; i++) {
tmp = array[i].split("=");
result[tmp[0]] = tmp[1];
}
@GingerBear
GingerBear / react-native-start-with-link.js
Last active January 5, 2023 01:19
start react native bunlder with link
//= require vendor/slick.js
//= require vendor/photoswipe.js
//= require vendor/photoswipe-ui-default.js
function SlickZoom(options) {
options = options || {};
if (!(options.selector && options.item)) {
return console.error('selector and item required');
}
@GingerBear
GingerBear / return-type-overloads.ts
Created May 16, 2019 18:42
return different type based parameter with overloads
function foo(): null;
function foo(a: number): string;
function foo(a?) {
if (typeof a === 'number') {
return '123';
} else {
return null;
}
}
@GingerBear
GingerBear / create-context-provider.tsx
Created September 27, 2019 17:57
Util function to create type safe state management store with useContext and useReducer
/**
*
* Util function to create type safe state management store with useContext and useReducer.
* The mechanism is described by https://kentcdodds.com/blog/how-to-use-react-context-effectively/
*
*/
import React, { useReducer, createContext, useContext } from 'react';
export type ContextProviderProps = { children: React.ReactNode };
@GingerBear
GingerBear / arr-filter-type-guard.ts
Created September 17, 2019 15:11
TS arr filter type guard
function notNullOrUndefined<T>(x: T | null | undefined): x is T {
return x !== null && x !== undefined;
}
const arr = [1, 2, null, undefined]; // (number | null | undefined)[]
const arrFiltered = arr.filter(notNullOrUndefined); // number[]
@GingerBear
GingerBear / sampleArray.js
Created April 2, 2019 19:13
sample Array
function sampleArray(arr, num) {
if (arr.length <= num) {
return arr;
} else {
const every = Math.floor(arr.length / num);
const sampleIndex = Array(num)
.fill(0)
.map((_, i) => i * every);
// keep last element
@GingerBear
GingerBear / Dockerfile
Last active April 1, 2019 20:38
alpine node3 with python 3 and puppeteer ready
FROM node:10-alpine
# Installs Python 3
RUN apk add --no-cache python3 && \
python3 -m ensurepip && \
rm -r /usr/lib/python*/ensurepip && \
pip3 install --upgrade pip setuptools && \
if [ ! -e /usr/bin/pip ]; then ln -s pip3 /usr/bin/pip ; fi && \
if [[ ! -e /usr/bin/python ]]; then ln -sf /usr/bin/python3 /usr/bin/python; fi && \
rm -r /root/.cache
@GingerBear
GingerBear / proxy_dev_server.js
Created March 7, 2019 21:15
proxy dev server
const express = require('express');
const path = require('path');
const fs = require('fs');
const proxy = require('http-proxy-middleware');
const app = express();
const proxyConfig = require('./proxy-remote.conf.json');
const port = 4200;
const ngBuildPath = path.join(__dirname, 'dist/projectAbc');
const ngBuildIndex = fs
@GingerBear
GingerBear / proxy_dev_server.js
Created March 7, 2019 21:15
proxy dev server
const express = require('express');
const path = require('path');
const fs = require('fs');
const proxy = require('http-proxy-middleware');
const app = express();
const proxyConfig = require('./proxy-remote.conf.json');
const port = 4200;
const ngBuildPath = path.join(__dirname, 'dist/projectAbc');
const ngBuildIndex = fs