Skip to content

Commit

Permalink
perf: save 0.2kB bundle size by const-ing error message (#21)
Browse files Browse the repository at this point in the history
* perf: save 0.2kB bundle size by const-ing error message

* i did run prettier this time

* add tests

* `throw new NotAllowedError` and improved error msg

---------

Co-authored-by: Bart Veneman <[email protected]>
  • Loading branch information
bartveneman and Bart Veneman authored Feb 18, 2024
1 parent 5c84df3 commit 771a237
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 5 deletions.
14 changes: 10 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import generate from 'css-tree/generator';

import { calculate } from './core/index.js';
import { compare, equals, greaterThan, lessThan } from './util/compare.js';
import { min, max } from './util/filter.js';
import { sortAsc, sortDesc } from './util/sort.js';

class NotAllowedError extends Error {
constructor() {
super('Manipulating a Specificity instance is not allowed. Instead, create a new Specificity()');
}
}

class Specificity {
constructor(value, selector = null) {
this.value = value;
Expand All @@ -15,23 +21,23 @@ class Specificity {
}

set a(val) {
throw new Error('Manipulating the port of the specificity directly is not allowed. Instead, directly set a new value');
throw new NotAllowedError();
}

get b() {
return this.value.b;
}

set b(val) {
throw new Error('Manipulating the port of the specificity directly is not allowed. Instead, directly set a new value');
throw new NotAllowedError();
}

get c() {
return this.value.c;
}

set c(val) {
throw new Error('Manipulating the port of the specificity directly is not allowed. Instead, directly set a new value');
throw new NotAllowedError();
}

selectorString() {
Expand Down
98 changes: 97 additions & 1 deletion test/types.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { deepEqual } from 'assert';
import { deepEqual, throws } from 'assert';
import Specificity from '../dist/index.js';

describe('Specificity Class, manual instance', () => {
Expand All @@ -18,6 +18,38 @@ describe('Specificity Class, manual instance', () => {
deepEqual(s.c, 3);
});
});
describe('Instance Setters', () => {
it('Specificity.a', () => {
throws(
() => {
s.a = 1;
},
{
message: 'Manipulating a Specificity instance is not allowed. Instead, create a new Specificity()',
}
);
});
it('Specificity.b', () => {
throws(
() => {
s.b = 1;
},
{
message: 'Manipulating a Specificity instance is not allowed. Instead, create a new Specificity()',
}
);
});
it('Specificity.c', () => {
throws(
() => {
s.c = 1;
},
{
message: 'Manipulating a Specificity instance is not allowed. Instead, create a new Specificity()',
}
);
});
});
describe('Value Formatting', () => {
it('Specificity.toString()', () => {
deepEqual(s.toString(), '(1,2,3)');
Expand Down Expand Up @@ -59,6 +91,38 @@ describe('Specificity Class, manual instance, no given selector', () => {
deepEqual(s.c, 3);
});
});
describe('Instance Setters', () => {
it('Specificity.a', () => {
throws(
() => {
s.a = 1;
},
{
message: 'Manipulating a Specificity instance is not allowed. Instead, create a new Specificity()',
}
);
});
it('Specificity.b', () => {
throws(
() => {
s.b = 1;
},
{
message: 'Manipulating a Specificity instance is not allowed. Instead, create a new Specificity()',
}
);
});
it('Specificity.c', () => {
throws(
() => {
s.c = 1;
},
{
message: 'Manipulating a Specificity instance is not allowed. Instead, create a new Specificity()',
}
);
});
});
describe('Value Formatting', () => {
it('Specificity.toString()', () => {
deepEqual(s.toString(), '(1,2,3)');
Expand Down Expand Up @@ -100,6 +164,38 @@ describe('Specificity Class', () => {
deepEqual(s.c, 3);
});
});
describe('Instance Setters', () => {
it('Specificity.a', () => {
throws(
() => {
s.a = 1;
},
{
message: 'Manipulating a Specificity instance is not allowed. Instead, create a new Specificity()',
}
);
});
it('Specificity.b', () => {
throws(
() => {
s.b = 1;
},
{
message: 'Manipulating a Specificity instance is not allowed. Instead, create a new Specificity()',
}
);
});
it('Specificity.c', () => {
throws(
() => {
s.c = 1;
},
{
message: 'Manipulating a Specificity instance is not allowed. Instead, create a new Specificity()',
}
);
});
});
describe('Value Formatting', () => {
it('Specificity.toString()', () => {
deepEqual(s.toString(), '(1,2,3)');
Expand Down

0 comments on commit 771a237

Please sign in to comment.