Wednesday, 30 October 2019

handling intersection between selection markings

I have a mark button on UI, clicking which, any user selection is marked red. No problems here. I achieve this by document.execCommand("insertHTML")

But I have an additional requirement that if the new selection is created which is the intersection of old selections markings, old selection's red marking should disappear.

As an example:

In the following image: this and testing are marked. Now if I select his is tes from the start and click mark, old markings of this and testing should go away and only his is tes should be marked because there is an intersection.

enter image description here

code:

const button = document.getElementById("button");

button.addEventListener('click', ()=>{
        const s = window.getSelection();
  const selectionStr = s.toString();
  document.execCommand("insertHTML", false, `<span class="bg-red">${selectionStr}<span>`);
})
.bg-red {  
background: red;
}
<div contenteditable="true">
 this is testing  this is testing  this is testing
</div>

<button id="button">mark</button>


from handling intersection between selection markings

How can I increment many fields at once when data structure matches Model exactly in Mongoose?

I currently have a schema with many fields, all of type number like this:

const mySchema = new mongoose.Schema({
    fields: {
        field1: {
            type: Number,
            default: 0
        },
        field2: {
            type: Number,
            default: 0
        },
        ...
        field20: {
            type: Number,
            default: 0
        }
    }
});

const MyModel = mongoose.model('model', mySchema);
module.exports = MyModel;

I currently increment all the fields individually:

const fields = {
    field1: 5,
    field2: 10,
    ...
    field20: 7
};

MyModel.findOneAndUpdate(
    {...},
    {
        $inc: {
            "fields.field1": fields.field1,
            "fields.field2": fields.field2,
            ...
            "fields.field20": fields.field20
        }
    }
);

Is there a shorthand way to increment all these fields at once? Something like below:

MyModel.findOneAndUpdate(
    {...},
    {
        $inc: {
            "fields": fields
        }
    }
);

This understandably gives the error

"Cannot increment with non-numeric argument: ..."



from How can I increment many fields at once when data structure matches Model exactly in Mongoose?

Android Unit Testing with Mockito

I have a ViewModel in which there is a method which has the following line of code:

billDate.set(!TextUtils.isEmpty(SampleApp.getInstance().getAccountManager().getDueDate()) ?
            String.format(SampleApp.getInstance().getApplicationContext().getString(R.string.due),
                    SampleApp.getInstance().getAccountManager().getBillingDueDate()) :
            SampleApp.getInstance().getApplicationContext().getString(R.string.missing_due_date));

I have a test class using Mockito to test the different methods in ViewModel. But it is failing with NullPointerException at this line

String.format(SampleApp.getInstance().getApplicationContext().getString(R.string.due),

Below is the log:

java.lang.NullPointerException
at java.util.regex.Matcher.getTextLength(Matcher.java:1283)
at java.util.regex.Matcher.reset(Matcher.java:309)
at java.util.regex.Matcher.<init>(Matcher.java:229)
at java.util.regex.Pattern.matcher(Pattern.java:1093)
at java.util.Formatter.parse(Formatter.java:2547)
at java.util.Formatter.format(Formatter.java:2501)
at java.util.Formatter.format(Formatter.java:2455)
at java.lang.String.format(String.java:2940)

While running test case, I see the log showing some error related to Pattern Can somebody suggest, how to test the String.format method?



from Android Unit Testing with Mockito