Skip to content

Instantly share code, notes, and snippets.

@urugator
urugator / useDerivedState.js
Last active April 16, 2019 17:45
useDerivedState
const React = require('react');
const __DEV__ = process.env.NODE_ENV !== 'production';
const HOOK_NAME = 'useDerivedState';
const NO_DEPS_HINT = 'If there are no dependencies, use "const [state] = useState(fn)"'
/**
* Copied from from React sources and adjusted
* inlined Object.is polyfill to avoid requiring consumers ship their own
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
// plugin.js
'use strict';
const BabelParser = require("@babel/parser");
// https://github.com/jamiebuilds/babel-handbook/blob/master/translations/en/plugin-handbook.md
// TODO import decorate/extendObservable
module.exports = function ({ types }) {
const visitor = {
/*
@urugator
urugator / regex.js
Last active December 28, 2021 09:05
export function escapeRegex(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
/**
* regex`${'escaped'} ${regex`nestable`}`
* regex('gi')`withFlags`
*/
export function regex(...args) {
function _regex(flags, strings, ...values) {
export function escapeRegex(string: string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
export function regex(
strings: TemplateStringsArray,
...values: Array<RegExp | string>
): RegExp;
export function regex(