Remove "framework"
This commit is contained in:
parent
b45bdb18a0
commit
dc86d30eb2
72 changed files with 359 additions and 333 deletions
8
src/helper/create-element.ts
Normal file
8
src/helper/create-element.ts
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
import { html } from '../types/html';
|
||||
|
||||
export const createElement = (from: html): HTMLElement => {
|
||||
// won't work for all elements, eg.: <td>
|
||||
const element: HTMLElement = document.createElement('div');
|
||||
element.innerHTML = from;
|
||||
return element.firstElementChild as HTMLElement;
|
||||
};
|
||||
11
src/helper/get-height.ts
Normal file
11
src/helper/get-height.ts
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
export const getHeight = (e: HTMLElement): number => {
|
||||
// ignores margin collapse
|
||||
const computedStyle = getComputedStyle(e);
|
||||
return (
|
||||
e.clientHeight +
|
||||
parseInt(computedStyle.marginTop) +
|
||||
parseInt(computedStyle.marginBottom) +
|
||||
parseInt(computedStyle.borderTopWidth) +
|
||||
parseInt(computedStyle.borderBottomWidth)
|
||||
);
|
||||
};
|
||||
2
src/helper/last.ts
Normal file
2
src/helper/last.ts
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
export const last = <T>(list: ArrayLike<T>): T | undefined =>
|
||||
list.length >= 1 ? list[list.length - 1] : undefined;
|
||||
33
src/helper/mix-colors.ts
Normal file
33
src/helper/mix-colors.ts
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
export type hex = string;
|
||||
export type rgb = [number, number, number];
|
||||
|
||||
export const mixColors = (hexColorA: hex, hexColorB: hex, quantityA: number): hex => {
|
||||
const colorA = hexToRGB(hexColorA);
|
||||
const colorB = hexToRGB(hexColorB);
|
||||
|
||||
const mixedColor: rgb = [
|
||||
mix(colorA[0], colorB[0], quantityA),
|
||||
mix(colorA[1], colorB[1], quantityA),
|
||||
mix(colorA[2], colorB[2], quantityA),
|
||||
];
|
||||
|
||||
return rgbToHex(mixedColor);
|
||||
};
|
||||
|
||||
const hexToRGB = (hex: hex): rgb => {
|
||||
const [r1, r2, g1, g2, b1, b2] = normalizeHex(hex);
|
||||
return [parseInt(r1 + r2, 16), parseInt(g1 + g2, 16), parseInt(b1 + b2, 16)];
|
||||
};
|
||||
|
||||
const normalizeHex = (hex: hex): hex => {
|
||||
hex = hex.trim();
|
||||
if (hex[0] === '#') {
|
||||
hex = hex.substr(1);
|
||||
}
|
||||
return hex;
|
||||
};
|
||||
|
||||
const mix = (a: number, b: number, q: number): number => a * q + b * (1 - q);
|
||||
|
||||
const rgbToHex = (rgb: rgb): hex =>
|
||||
'#' + rgb.map(n => (n < 16 ? '0' : '') + Math.round(n).toString(16)).join('');
|
||||
17
src/helper/polyfill-imul.ts
Normal file
17
src/helper/polyfill-imul.ts
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
export const polyfillImul = () => {
|
||||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/imul
|
||||
if (!Math.imul)
|
||||
Math.imul = function(opA, opB) {
|
||||
opB |= 0; // ensure that opB is an integer. opA will automatically be coerced.
|
||||
// floating points give us 53 bits of precision to work with plus 1 sign bit
|
||||
// automatically handled for our convenience:
|
||||
// 1. 0x003fffff /*opA & 0x000fffff*/ * 0x7fffffff /*opB*/ = 0x1fffff7fc00001
|
||||
// 0x1fffff7fc00001 < Number.MAX_SAFE_INTEGER /*0x1fffffffffffff*/
|
||||
let result = (opA & 0x003fffff) * opB;
|
||||
// 2. We can remove an integer coercion from the statement above because:
|
||||
// 0x1fffff7fc00001 + 0xffc00000 = 0x1fffffff800001
|
||||
// 0x1fffffff800001 < Number.MAX_SAFE_INTEGER /*0x1fffffffffffff*/
|
||||
if (opA & 0xffc00000 /*!== 0*/) result += ((opA & 0xffc00000) * opB) | 0;
|
||||
return result | 0;
|
||||
};
|
||||
};
|
||||
20
src/helper/random.ts
Normal file
20
src/helper/random.ts
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import { polyfillImul } from './polyfill-imul';
|
||||
|
||||
export class Random {
|
||||
public constructor(private seed: number) {
|
||||
polyfillImul();
|
||||
}
|
||||
|
||||
public get next(): number {
|
||||
// result is in [0, 1)
|
||||
return ((2 ** 31 - 1) & (this.seed = Math.imul(48271, this.seed))) / 2 ** 31;
|
||||
}
|
||||
|
||||
public choose<T>(list: Array<T>): T {
|
||||
return list[Math.floor(this.randomInInterval(0, list.length))];
|
||||
}
|
||||
|
||||
public randomInInterval(aClosed: number, bOpen: number): number {
|
||||
return (bOpen - aClosed) * this.next + aClosed;
|
||||
}
|
||||
}
|
||||
2
src/helper/sum.ts
Normal file
2
src/helper/sum.ts
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
export const sum = (list: ArrayLike<number>): number =>
|
||||
Array.prototype.reduce.call(list, (a: number, sum: number) => a + sum, 0);
|
||||
Loading…
Add table
Add a link
Reference in a new issue