Skip to content

Commit 6f692d7

Browse files
committed
add set custom values
1 parent bdaa332 commit 6f692d7

File tree

2 files changed

+69
-4
lines changed

2 files changed

+69
-4
lines changed

lib/options/selectorLibrary.js

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,15 @@ class SelectorLibrary {
216216
*
217217
* @param {String | Array} value this could be either a css selector or an array of css selectors
218218
*/
219-
set(value, options) {
219+
set(value, renamedSelector, options) {
220220
let selectorObject;
221221
let selectorLibrarySelector;
222222

223+
if (typeof renamedSelector !== 'string') {
224+
options = renamedSelector;
225+
renamedSelector = undefined;
226+
}
227+
223228
options = options || {};
224229

225230
// loops through String.match array
@@ -258,7 +263,7 @@ class SelectorLibrary {
258263
}
259264

260265
// save css selector into this.selectors and this.compressedSelectors
261-
this.selectors[selectorLibrarySelector] = this.setValue(value, options);
266+
this.selectors[selectorLibrarySelector] = this.setValue(value, renamedSelector, options);
262267
this.compressedSelectors[this.selectors[selectorLibrarySelector].compressedSelector] = this.selectors[selectorLibrarySelector];
263268

264269
return;
@@ -302,13 +307,22 @@ class SelectorLibrary {
302307
*
303308
* @return {Object} the object to set it into the set method
304309
*/
305-
setValue(string, options) {
310+
setValue(string, renamedSelector, options) {
306311
let type;
307312
let selector;
308313
let typeChar;
309314
let compressedSelector;
310315
let selectorLibrarySelector = string.slice(1, string.length);
311316

317+
if (typeof renamedSelector !== 'string') {
318+
options = renamedSelector;
319+
renamedSelector = undefined;
320+
} else {
321+
if (renamedSelector.charAt(0) === '.' || renamedSelector.charAt(0) === '#') {
322+
renamedSelector = renamedSelector.slice(1, renamedSelector.length);
323+
}
324+
}
325+
312326
options = options || {};
313327

314328
// return the own value if it exists
@@ -319,7 +333,9 @@ class SelectorLibrary {
319333
type = string.charAt(0) === '.' ? 'class' : 'id';
320334
typeChar = string.charAt(0);
321335
selector = string;
322-
compressedSelector = rcs.nameGenerator.generate();
336+
compressedSelector = renamedSelector || rcs.nameGenerator.generate();
337+
338+
// @todo while loop compressedSelector as long as it does not already exist
323339

324340
// options time
325341
if (options.preventRandomName === true) {
@@ -342,6 +358,23 @@ class SelectorLibrary {
342358
compressedSelector
343359
};
344360
} // /setValue
361+
362+
/**
363+
* set multiple values at once
364+
*
365+
* @param {object} selectors these selectors should have the selector as key and the modified selector as value
366+
*/
367+
setValues(selectors) {
368+
if (Object.prototype.toString.call(selectors) !== '[object Object]') {
369+
throw 'setValues just accept an object as parameter';
370+
}
371+
372+
for (let key in selectors) {
373+
let value = selectors[key];
374+
375+
this.set(key, value);
376+
}
377+
} // /setValues
345378
};
346379

347380
/**

test/selectorLibrary.spec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,24 @@ describe('rcs selector library', () => {
2323
done();
2424
});
2525

26+
it('should set a new custom value', done => {
27+
rcs.selectorLibrary.set('.test', 'random-name');
28+
29+
expect(rcs.selectorLibrary.get('.test')).to.equal('random-name');
30+
expect(rcs.selectorLibrary.get('test')).to.equal('random-name');
31+
32+
done();
33+
});
34+
35+
it('should set a new custom value with selector type', done => {
36+
rcs.selectorLibrary.set('.test', '.random-name');
37+
38+
expect(rcs.selectorLibrary.get('.test')).to.equal('random-name');
39+
expect(rcs.selectorLibrary.get('test')).to.equal('random-name');
40+
41+
done();
42+
});
43+
2644
it('should set an object value', done => {
2745
const setValueObject = rcs.selectorLibrary.setValue('.test');
2846

@@ -33,6 +51,20 @@ describe('rcs selector library', () => {
3351
done();
3452
});
3553

54+
it('should set multiple values', done => {
55+
rcs.selectorLibrary.setValues({
56+
'.test': 'a',
57+
'.class': '.b',
58+
'.selector': 'c'
59+
});
60+
61+
expect(rcs.selectorLibrary.get('test')).to.equal('a');
62+
expect(rcs.selectorLibrary.get('.class')).to.equal('b');
63+
expect(rcs.selectorLibrary.get('.selector')).to.equal('c');
64+
65+
done();
66+
});
67+
3668
it('should set values out of an array', done => {
3769
rcs.selectorLibrary.set([
3870
'.test',

0 commit comments

Comments
 (0)