Skip to content

Instantly share code, notes, and snippets.

View mohdovais's full-sized avatar

Mohd Ovais mohdovais

View GitHub Profile
@mohdovais
mohdovais / rollup-plugin-import-umd.js
Last active February 10, 2023 21:28
Rollup.js plugin to import UMD modules. It exports umd function expression as default in esm
import { normalize } from "path";
import { readFileSync } from "fs";
import { isRegExp } from "util";
import { parse } from "acorn";
const toArray = item => Array.isArray(item) ? item : [item];
// https://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript#answer-3561711
const escapeRegexpStr = str => str.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
/**
* Assign nested properties from `copy` to `obj`
* @template O, C object and copy
* @param {O} obj The object to copy properties to
* @param {C} copy The object to copy properties from
* @returns {O & C} a new object
*/
function assign(obj, copy) {
var draft = {};
var O = Object;
@mohdovais
mohdovais / svelte-store.js
Last active December 24, 2019 17:56
Redux style store with svelte/store
/**
* Svelte does not compare objects
* https://github.com/sveltejs/svelte/issues/2171
* and therefore store.update notifies subscribers, even if same object is
* returned. Otherwise the method could be much simpler:
* const dispatch = action => update(state => reducer(state, action));
*
* @param {function} reducer
* @param {*} initialState
*/
@mohdovais
mohdovais / copy-to-clipboard.js
Created August 31, 2019 20:00
copy text to clipboard in browser
function copyToClipboard(text){
var textarea = document.createElement('textarea');
textarea.value = text;
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
document.body.removeChild(textarea);
}
@mohdovais
mohdovais / pure.js
Last active August 14, 2019 09:03
naive React v15 pure functional component
import React from 'react';
const pureMap = new WeakMap();
function makeComponent(fn, compare) {
return class Memo extends React.Component {
shouldComponentUpdate(nextProps) {
return compare(this.props, nextProps);
}
render() {
@mohdovais
mohdovais / rollup-plugin-custom-umd.js
Created August 5, 2019 06:20
Rollup plugin to create custom UMD package; the module is always exposed as global variable
const { parse } = require("acorn");
const expressionStatement = node => node.type === "ExpressionStatement";
function chainedProperty(subject, chainString) {
const chain = Array.isArray(chainString)
? chainString
: chainString.split(".");
const count = chain.length;
@mohdovais
mohdovais / parser.php
Last active August 29, 2015 14:15
A parser to fetch records from http://eciresults.nic.in and convert into JSON
<?php
//https://github.com/mohdovais/Delhi-Elections-2015
spl_autoload_register(
function($className)
{
$className = str_replace("_", "\\", $className);
$className = ltrim($className, '\\');
$fileName = '';