Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
David Luna committed Nov 5, 2020
2 parents dedd5c6 + b09a0ee commit 9abc223
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "csset",
"version": "0.0.4",
"version": "0.0.5",
"description": "experiment to perform algebra of sets with css",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down
45 changes: 45 additions & 0 deletions src/csset.spec.ts
Expand Up @@ -380,4 +380,49 @@ describe('Csset', () => {
});
});
});

describe('union', () => {
test('should return one set if is superset of the other', () => {
const data = [
{
sel1: 'div, p, aside, section',
sel2: 'div, p, section.class',
expected: 'div,p,aside,section',
},
{
sel1: 'div, p, section.class',
sel2: 'div, p, aside, section',
expected: 'div,p,aside,section',
},
];

data.forEach( d => {
const set1 = new Csset(d.sel1);
const set2 = new Csset(d.sel2);
const result = `${set1} ${operationSymbols.union} ${set2} => ${set1.union(set2)}`;
const expected = `${set1} ${operationSymbols.union} ${set2} => ${d.expected}`;

expect(result).toEqual(expected);
});
});

test('should remove rules that are subset of others', () => {
const data = [
{
sel1: 'div, p, aside, section.class',
sel2: 'div, p#id, span, a, section',
expected: 'div,p,aside,span,a,section',
},
];

data.forEach( d => {
const set1 = new Csset(d.sel1);
const set2 = new Csset(d.sel2);
const result = `${set1} ${operationSymbols.union} ${set2} => ${set1.union(set2)}`;
const expected = `${set1} ${operationSymbols.union} ${set2} => ${d.expected}`;

expect(result).toEqual(expected);
});
});
});
});
31 changes: 30 additions & 1 deletion src/csset.ts
Expand Up @@ -86,7 +86,36 @@ export class Csset {
* @param set the set to check with
*/
subsetOf(set: Csset): boolean {
return set.subsetOf(this);
return set.supersetOf(this);
}

union(set: Csset): Csset {
if (this.supersetOf(set)) {
return this;
}

if (this.subsetOf(set)) {
return set;
}

// return new Csset('unknown');

// If one of the sets does not have subsets just return a set with all
if (this.layers || set.layers) {
return new Csset(`${this},${set}`);
}

// Make union in subsets if possible
const equalSets = this.subsets.filter(thisSet => set.subsets.some(otherSet => `${thisSet}` === `${otherSet}`));
const uniqueOne = this.subsets.filter(s => !s.subsetOf(set));
const uniqueTwo = set.subsets.filter(s => !s.subsetOf(this));

const equSelector = equalSets.map(s => `${s}`).join(',');
const oneSelector = uniqueOne.map(s => `${s}`).join(',');
const twoSelector = uniqueTwo.map(s => `${s}`).join(',');


return new Csset(`${equSelector},${oneSelector},${twoSelector}`);
}

toString(): string {
Expand Down

0 comments on commit 9abc223

Please sign in to comment.