Skip to content

Instantly share code, notes, and snippets.

View vitaly-t's full-sized avatar
🐈
Meow!

Vitaly Tomilov vitaly-t

🐈
Meow!
View GitHub Profile
// Works the same as JSON.parse, but also handles BigInt type,
// by returning BigInt for each string that uses 123n format:
function parse(text) {
return JSON.parse(text, (_, value) => {
if (typeof value === 'string') {
const m = value.match(/(-?\d+)n/);
if (m && m[0] === value) {
value = BigInt(m[1]);
}
// Works the same as JSON.parse, but also handles BigInt type,
// by returning BigInt for each string that uses 123n format:
function parse(text) {
return JSON.parse(text, (_, value) => {
if (typeof value === 'string') {
const m = value.match(/(-?\d+)n/);
if (m && m[0] === value) {
value = BigInt(m[1]);
}
}
function parse(text) {
return JSON.parse(text, (_, value) => {
if (typeof value === 'string') {
const m = value.match(/(-?\d+)n/);
if (m && m[0] === value) {
value = BigInt(m[1]);
}
}
return value;
});
// Alternative/official approach to serializing BigInt:
BigInt.prototype.toJSON = function () {
return `${this.toString()}n`;
};
// Alternative/official approach to serializing BigInt:
BigInt.prototype.toJSON = function () {
return `${this.toString()}n`;
};
const header = require('./db/header');
const promise = header.defPromise;
const options = {
promiseLib: promise,
noWarnings: true
};
const dbHeader = header(options);
const pgp = dbHeader.pgp;
const db = dbHeader.db;

Is there a regular expression to detect a valid regular expression?.

Deleted from StackOverflow by the author, because oddly, it got many downvotes.


Reflecting on this in 2016, in the way that things changed since...

The direct answer to the question is still the resounding NO, it is impossible to implement a regular expression to find all regular expressions in your code.

/**
* Drains the source observable till it completes, and then posts a new value-observable.
*/
function drain<T>(value: T | Observable<T> | (() => T | Observable<T>)) {
const v = () => {
const a = typeof value === 'function' ? value.call(null) : value;
return a instanceof Observable ? a : of(a);
}
return s => defer(() => s.pipe(filter(_ => false), c => concat(c, v()))) as Observable<T>;
}
function sieveOddPrimesTo(bufferLimit: number) {
const lmti = (bufferLimit - 3) >> 1;
const sz = (lmti >> 3) + 1;
const cmpSts = new Uint8Array(sz);
for (let i = 0; ; ++i) {
const p = i + i + 3;
const sqri = (i << 1) * (i + 3) + 3;
if (sqri > lmti) {
break;
}
import {generatePrimes} from 'primes-generator';
// there are 1,270,607 primes in the first 20mln:
const iterator = generatePrimes({boost: 1_270_607});
let a, lastValue = 0, count = 0;
const start = Date.now();
do {
a = iterator.next();