I'm using jQuery and have retrieved a string containing all the classes of an element like this:
var classes = $('[class^="menu-icon-"]').attr('class');
The string prints something like this: "foo menu-icon-icon-name bar"
How can find out what the "icon-name" part in the string is, whatever it may be?
It will always be with a class formatted as "menu-icon-{icon-name-here}" among whatever other classes the element may contain.
This works in all cases-
var menuname = classes.substr(classes.indexOf("menu-icon") + 10).split(" ")[0];
lets say you have one div's classes as a string
var class = "foo menu-icon-icon-name bar"
if you are certain that there will always be a space after "" you should be able to do something like
var name = class.match(/menu-icon-(.*) /)[1];
Related
I am looking for an elegant way to convert a List where we remove the object information and keep only a specific property.
example: List< Fruit> becomes List with the property fruitColor (a String) kept.
I think you're looking for:
var listOfFruit = ...;
var listOfColors = listOfFruit.map((e) => e.fruitColor).toList();
I'm having trouble printing localized text conditionally. For example, this localizes properly:
if valueFromDb.isEmpty {
Text("value_is_empty") //localized text
} else {
Text(valueFromDb)
}
It prints some text in the user's language if valueFromDb is empty, or it prints valueFromDb as it is if it's not. However, when I try to use the ternary operator it doesn't work:
Text(valueFromDb.isEmpty ? "value_is_empty" : valueFromDb)
When valueFromDb is empty, it prints "value_is_empty" rather than actual localized text. I get an error (a random one higher up in the hierarchy thanks to SwiftUI) when trying to cast it as LocalizedStringKey.
Edit: To be clear, I know I can do this:
valueFromDb.isEmpty ? Text("value_is_empty") : Text(valueFromDb)
However, I want to put the ternary conditional inside the Text() brackets because I will do this for several views, and each one will have many modifiers, so the code will become quite bloated.
The problem is due to type inference. You have to declare myString to be of type LocalizedStringKey and then everything will work as expected.
When you declare:
#State var mySrtring: LocalizedStringKey = "whatever"
Then:
Text(myString.isEmpty ? "error_text_localized" : myString)
uses this initializer:
public init(_ key: LocalizedStringKey,
tableName: String? = nil,
bundle: Bundle? = nil,
comment: StaticString? = nil)
When you declare it like this:
#State var mySrtring: String = "whatever"
Then:
Text(myString.isEmpty ? "error_text_localized" : myString)
uses this initialiser:
public init(verbatim content: String)
You have to put your valueFromDb in quotations, then it should work fine.
Text(valueFromDb.isEmpty ? "value_is_empty" : "\(valueFromDb)")
Looking to create an enum for some regex to store in my application to allow for code reuse.
Eg:
export enum Regex {
ONE_DANK_REGEX = /^[dank]+$/g,
FIVE_OUT_OF_SEVEN = /^[meme]{5,7}$/g
}
But:
By not using string literals, I get the TS90010 error: Type RegExpis not assignable to type Regex.
By using string literals, I can't use these in a pattern attribute on my inputs
Eg.
<input ngModel="..." pattern="{{FIVE_OUT_OF_SEVEN}}" .../>
Is this the right way of going about doing this?
You cannot assign a RegExp to an enum, an enum can either be numeric or string-based.
So when it comes to storing the RegExp you have two options:
Option A - store the RegExp as string in an enum
enum RegExpEnum {
ONE_DANK_REGEX = "^[dank]+$",
FIVE_OUT_OF_SEVEN = "^[meme]{5,7}$"
}
Option B - store the RegExp in a class/variable
class RegexClass {
public static readonly ONE_DANK_REGEX = /^[dank]+$/g;
public static readonly FIVE_OUT_OF_SEVEN = /^[meme]{5,7}$/g;
}
In both ways you need to get a reference to the containing enum or class in your component. Which can be achieved by assigning it to a local variable.
#Component({ ... })
public class MyComponent {
availableRegex = RegExpEnum; // or RegexClass
}
Then you can access it in the html part of your component.
<input [(ngModel)]="name" [pattern]="availableRegex.ONE_DANK_REGEX"/>
DEMO
Enum will support only strings and Number.
If you wanted to store regular expressions, you need to use static class
export class RegularExpression
{
public static ALPHA_CHARACTERS: RegExp = /([^A-Za-z])+/g;
}
Usage
'Your_* Name'.replace(RegularExpression.ALPHA_CHARACTERS, '');
Prefer the option A mentioned on the currently accepted answer and use it as below the code:
export enum Regex {
ONE_DANK_REGEX = "^[dank]+$",
FIVE_OUT_OF_SEVEN = "^[meme]{5,7}$",
}
public readonly regOne = new RegExp(Regex.ONE_DANK_REGEX, "g");
This gives some flexibility when create the regular expression object.
The question is probably best asked with a simple example:
var myObj = { name: 'John' };
var copiedObj = ObjectCopier.copy(myObj);
copiedObj.name.should.equal('John'); // Hard code 'John' twice
copiedObj.name.should.equal(myObj.name); // Reference the original value
Is one method preferred over the other? Assuming the value passed in is what I expect to be returned, is there any harm in the 2nd assert? Does it even matter?
In more complex cases you won't be able to duplicate an object completely - and you wouldn't want to. it would be better written this way:
var OBJ_NAME = 'John'
var myObj = { name: OBJ_NAME };
var copiedObj = ObjectCopier.copy(myObj);
copiedObj.name.should.equal(OBJ_NAME);
this way you're not duplicating any code/defines, and you can also make tests such as:
myObj.name.should.equal(OBJ_NAME);
to test for the object copier not changing the original object either (which either of your lines won't test for).
This test is OK, because I use toString explicitly
"have one sentence by '...'" in {
val text1 = Text("some text...")
text1.sentences must have size(1)
text1.sentences(0).toString must_== "some text"
}
If without toSting the test it fails with message like:
Expected :some text
Actual :some text
java.lang.Exception: 'some text: dictionary.Sentence' is not equal to 'some text: java.lang.String'
I understand the sense of it (in general), but since toString is
invoked anyway shouldn't it check string to string then?
What is the best way to write this test to be concise? Without using
toString directly.
I think you might be confusing toString and equals. When you say what you wanted to say:
text1.sentences(0) must_== "some text"
What you are really saying is:
text1.sentences(0).equals("some text") must beTrue
If you want this to work, then you would need there to be an equals function on the Sentence class that used the toString of the sentence to compare to the incoming object (a String in this case). A simple spec showing that could look like this:
class Sentence(text:String){
override def equals(obj:Any) = {
this.toString == obj.toString
}
override def toString = text
}
class EqualitySpec extends Specification{
"A sentence" should{
"be equal to plain text" in {
val sentence = new Sentence("hello world")
sentence must be_==("hello world")
}
}
}
Now this works great if that Sentence class is your own class. If it's in a third party library and you have no control over the equals function then you might be stuck with things the way that they are.