remove: remove class and style utility functions (use Element.classList and HTMLElement.style instead)

This commit is contained in:
Xymorot 2020-06-07 16:40:32 +02:00
parent 642ae830a6
commit 8bb0a0220b
2 changed files with 1 additions and 103 deletions

View File

@ -1,7 +1,6 @@
import { expect } from 'chai';
import fc from 'fast-check';
import 'mocha';
import { c, s, t } from './utils';
import { t } from './utils';
describe('Frontend Utils', function () {
this.timeout(1000);
@ -9,89 +8,4 @@ describe('Frontend Utils', function () {
it("does 'translate' text", () => {
expect(t('translate this')).to.equal('translate this');
});
describe('Class Util', () => {
beforeEach(function () {
const numeric = fc.integer(0x30, 0x39);
const alphaCapital = fc.integer(0x41, 0x5a);
const alphaSmall = fc.integer(0x61, 0x7a);
const alpha = fc.oneof(alphaCapital, alphaSmall).map(String.fromCharCode);
const alphaNumeric = fc.oneof(numeric, alphaCapital, alphaSmall).map(String.fromCharCode);
this.className = fc
.stringOf(fc.oneof(alpha, alphaNumeric, fc.constantFrom('-', '_')))
.filter((name: string) => /[a-zA-Z]/.test(name.charAt(0)));
this.anything = fc
.anything({
maxDepth: 1,
maxKeys: 2,
})
.filter((thing) => typeof thing !== 'string');
});
it('compiles classes out of an array', function () {
const classArrayArbitrary = fc.array(fc.oneof(this.className, this.anything));
fc.assert(
fc.property(classArrayArbitrary, (classArray) => {
const expectedClasses = classArray.filter((potentialClass) => typeof potentialClass === 'string');
const compiled = c(classArray);
expect(compiled).to.be.a('string', 'class util does not return a string');
expectedClasses.forEach((expectedClassName) => {
expect(compiled).to.contain(expectedClassName, 'class util does not add all classes');
});
})
);
});
it('compiles classes out of an object', function () {
const classObjectArbitrary = fc.object({
maxDepth: 0,
key: this.className,
values: [fc.boolean()],
});
fc.assert(
fc.property(classObjectArbitrary, (classObject: any) => {
const expectedClasses = Object.keys(classObject).filter((key) => classObject[key]);
const compiled = c(classObject);
expect(compiled).to.be.a('string', 'class util does not return a string');
expectedClasses.forEach((className) => {
expect(compiled.split(' ')).to.include(className, 'class util does not add all classes');
});
})
);
});
});
describe('Style Util', () => {
it('compiles an object into an html style property', () => {
const styleObjectArbitrary = fc.object({
maxDepth: 0,
key: fc.string(1, 10).filter((str) => !/\s/.test(str)),
values: [fc.string(), fc.integer(), fc.boolean()],
});
/\s/g.exec('');
fc.assert(
fc.property(styleObjectArbitrary, (styleObject: any) => {
const expectedString = Object.keys(styleObject)
.filter((key) => {
const value = styleObject[key];
if (typeof value === 'string') {
return !!value;
}
return typeof value === 'number';
})
.map((key) => `${key}:${styleObject[key]}`)
.join(';');
expect(s(styleObject)).to.equal(expectedString);
})
);
});
});
});

View File

@ -1,19 +1,3 @@
export function c(input: Array<string | boolean> | object): string {
const array = Array.isArray(input)
? input.filter((el: any) => typeof el === 'string')
: Object.keys(input).filter((key: keyof object) => !!input[key]);
return array.join(' ');
}
export function s(styles: object): string {
return Object.keys(styles)
.filter(
(key: keyof object) => typeof styles[key] === 'number' || (typeof styles[key] === 'string' && !!styles[key])
)
.map((key: keyof object) => `${key}:${styles[key]}`)
.join(';');
}
export function t(text: string): string {
// If you want to implement frontend translation, begin here.
return text;