How to add a selector to an existing rule? - postcss

Is there a way to add a selector to an existing rule with Postcss API?
Something like myRule.addSelector(".newSelector")?
Currently I have to do this:
myRule.selector = myRule.selector ? myRule.selector + ", .newSelector" : ".newSelector";

You can use rule.selectors, which is a property with a getter that returns an array of selectors (generated from the rule.selector string), and a setter that takes an array of selectors (and overwrites the rule.selector string).
var sels = myRule.selectors; //save the getter returned array so we can modify it with push.
sels.push('.newSelector'); //push returns the new length not the array, that's why we had to save the array in sels.
myRule.selectors = sels; //the setter will join the array elements and overwrite myRule.selector
A much shorter version using concat (because concat returns a new array).
myRule.selectors = myRule.selectors.concat('.newSelector');
See the PostCSS source here.

Related

proper way to get the last element of the array when split is used on the string within JSON

I have a JSON response from the server and I am using map to use only necessary key:valuepairs in Angular (typescript) that will be used to display on the Frontend side.
here bizStep is actually according to a standard (EPCIS) and has the following value:
urn:epcglobal:cbv:bizstep:receiving
I only want to the user to read receiving hence I used split and obtained the last value of the array to display the value.
The logic is shown below:
this.serv.getEpcisInfo(code) // HTTP GET Service from Angular
.subscribe(res => {
this.data = res.map(el => { // map only some key value pairs now!
return {
'business step': el.bizStep.split(':')[el.bizStep.split(':').length - 1]
});
});
But it is observed that in order to obtain the overall length of the splited string array I have to write the expression el.bizStep.split(':') twice.
Is there a shorthand or elegant expression to obtain the last string value of the array.
I did try to use el.bizStep.split(':')[-1] however this expression failed and did not provide me any value.
You can use Array.pop since you don't need to preserve the result of the split, i.e. el.bizStep.split(':').pop().
A more general approach would be to use an anonymous function, e.g.:
(s => s[s.length-1])(el.bizStep.split(':'))
You could modify this to get elements other than the last. Of course, this example has no error checking on the length or type of el.bizStep.

putting CLASSES back in array as3

I suppose I need to be able to get the name of the class an object belongs to.
1: I have two classes
CandyBar
BottledWater
2: I put their names in two arrays
var foodArray(CandyBar, BottledWater);
var waterArray();
3: I add children to stage using the array; like this
var foodItem = new foodArray[i];
4: I later try and move the reference for BottledWater to the waterArray, but can't. I'm not creating any new object or anything, just moving the reference from one array to another. Furthermore, I cant remove the reference either.
I suppose I could say I am adding children from an array, then wanting to push a reference to that "class" into another array, while removing the reference from the original array.
The proper way to make an Array variable is:
var foodArray: Array = [CandyBar, BottledWater];
var waterArray: Array = new Array();
than you can do
waterArray.push( new foodArray[0] );
or
var foodItem = new foodArray[i]();

How to creata an array of JEditorPane

I want to create an array of JEditorPane depending on the size of a String array.
Is there a possibility to create an array of JEditorPane? If yes, how?
Here is an example:
String [] elements = {"0","1","2","3","4"};
JEditorPane ePane [] = new JEditorPane[5];
I want to to put each String element into the certain JEditPane, i.e
JEditorPane[0].setText(elements[0]);
etc. But I get a nullpointerexception when run I run.
Your problem is, that Java initializes a new array with the default value for the given type. In this case it is null, because the JEditorPane inherits from Object.
You cannot call a method on null - that is where the NullPointerException comes from.
The solution: make a loop in which you initialize the JEditorPane-objects in the array.
Then you can do JEditorPane[0].setText(elements[0]);

How to replace content element in an Array Controller

Currently working on part of the app that requires cloning an element from the content, then modifying cloned element and saving back to model. I am having a problem when saving the cloned element and having to replace the old item with cloned one. What I am currently doing is changing all the properties of the old item like so (it works):
Blocks.replace = function(item1, item2) {
for(var k in item2) {
Ember.set(item1, k, item2[k]);
}
};
var selectedEmployment = this.get("controllers.employmentDataEntry").get("selectedEmployment");
var modelItem = content.findBy("#id", selectedEmployment["#id"]);
Blocks.replace(modelItem, selectedEmployment);
I'm trying to use the ArrayController replaceContent method, but I get an error saying "Invalid array length" when trying to run the following code:
var employmentIndex = content.indexOf(modelItem);
this.replaceContent(employmentIndex, 0, selectedEmployment);
Am I doing this incorrectly? Is there a better way of replacing an item?
Note: I am using JSON as a model. The ArrayController that is used when calling replaceContent contains an array of length 2.
Fix: Need to send in an array to replaceContent method. So change selectedEmployment to [SelectedEmployment]. Also, change 0 to 1, otherwise, content will end up having both element and cloned element.
this.replaceContent(employmentIndex, 1, [selectedEmployment]);

Is it possible to store an Array into an EncryptedLocalStore item? AIR

I want to save my Array's strucure and load it the next time I open my AIR application. Is there a way to store it to an EncryptedLocalStore item then get it later when I re-open the app?
EncryptedLocalStore.setItem() method takes a byte array when storing contents. To store an array, just use ByteArray.writeObject() method (as described in http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/ByteArray.html#writeObject()) to convert your Array to a ByteArray - and then persist the same to the ELS.
var array:Array = getArray();
var byteArray:ByteArray = new ByteArray();
byteArray.writeObject(array);
EncryptedLocalStore.setItem('somekey', byteArray);
Hope this helps.
Update: Added code to retrieve the array back.
var byteArray:ByteArray = EncryptedLocalStore.getItem('somekey');
var array:Array = byteArray.readObject() as Array;
Update: For custom classes.
In case you want to serialize your own custom classes to the ByteArray, you may have to call registerClassAlias() before writing the object to the ByteArray. For eg.
registerClassAlias("com.example.eg", ExampleClass);
I have found that it is easiest to to serialize the Array to a string and then store that string in the ELS. Then when you pull it out deserialize it back into an Array.

Resources