From 4a192c45f70a441d6904ba744aa74d6fda5c3b84 Mon Sep 17 00:00:00 2001 From: David Luna Date: Thu, 5 Nov 2020 01:32:33 +0100 Subject: [PATCH 1/3] feat: added union method --- src/csset.spec.ts | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/csset.ts | 28 +++++++++++++++++++++++++++- 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/src/csset.spec.ts b/src/csset.spec.ts index 7313acb..89ec330 100644 --- a/src/csset.spec.ts +++ b/src/csset.spec.ts @@ -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 subse of others', () => { + const data = [ + { + sel1: 'div, p, aside, section.class', + sel2: 'div, p#id, span, a, section', + expected: 'div,p,aside,section,span,a', + }, + ]; + + 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); + }); + }); + }); }); diff --git a/src/csset.ts b/src/csset.ts index c73db92..84eb9ab 100644 --- a/src/csset.ts +++ b/src/csset.ts @@ -86,7 +86,33 @@ 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 outliersOne = this.subsets.filter(s => !s.subsetOf(set)); + const outliersTwo = set.subsets.filter(s => !s.subsetOf(this)); + const oneSelector = outliersOne.map(s => `${s}`).join(','); + const twoSelector = outliersTwo.map(s => `${s}`).join(','); + + + return new Csset(`${oneSelector},${twoSelector}`); } toString(): string { From 80220975ca7d791364ba7e533fdf3d281c871722 Mon Sep 17 00:00:00 2001 From: David Luna Date: Thu, 5 Nov 2020 13:41:33 +0100 Subject: [PATCH 2/3] fix: equal selectors being swallowed --- src/csset.spec.ts | 4 ++-- src/csset.ts | 13 ++++++++----- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/csset.spec.ts b/src/csset.spec.ts index 89ec330..b3e7ba9 100644 --- a/src/csset.spec.ts +++ b/src/csset.spec.ts @@ -406,12 +406,12 @@ describe('Csset', () => { }); }); - test('should remove rules that are subse of others', () => { + 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,section,span,a', + expected: 'div,p,aside,span,a,section', }, ]; diff --git a/src/csset.ts b/src/csset.ts index 84eb9ab..bda07d2 100644 --- a/src/csset.ts +++ b/src/csset.ts @@ -106,13 +106,16 @@ export class Csset { } // Make union in subsets if possible - const outliersOne = this.subsets.filter(s => !s.subsetOf(set)); - const outliersTwo = set.subsets.filter(s => !s.subsetOf(this)); - const oneSelector = outliersOne.map(s => `${s}`).join(','); - const twoSelector = outliersTwo.map(s => `${s}`).join(','); + 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(`${oneSelector},${twoSelector}`); + return new Csset(`${equSelector},${oneSelector},${twoSelector}`); } toString(): string { From b09a0ee0576871e98be38010b0b11a417ef2fa49 Mon Sep 17 00:00:00 2001 From: David Luna Date: Thu, 5 Nov 2020 13:42:17 +0100 Subject: [PATCH 3/3] chore: bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index dade2c1..79e1c3f 100644 --- a/package.json +++ b/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",