CouchDB: View to return only certain documents? - mapreduce

I have (tried) to write a view to identify documents with an "otherCauseForRelease" attribute AND that attribute is actually populated. My View code is:
function (doc) {
if(doc.payload.otherCauseForRelease.length > 5); emit(doc.payload.otherCauseForRelease);
}
However, the return set includes documents with attribute values like "" (an open double-quotation followed by a close double-quotation). How can I exclude these documents from my results?

Try with this one here :
function (doc) {
if(doc.payload.otherCauseForRelease.length > 5)
emit(doc.payload.otherCauseForRelease);
}
You basically add an extra ; at the end of your if. Doing so, it didn't consider the next statement as the body of the if.
Another example with curly braces:
function (doc) {
if(doc.payload.otherCauseForRelease.length > 5){
emit(doc.payload.otherCauseForRelease);
}
}

Related

How to Set Multple Regex Costraints on textbox in ZKOSS

I have a textbox which should only accept Characters:-for that first regex has been set in constraint and it should not accept some reserved keywords that are A,R,F,U .Since two different constraints are set ,i want user to see the specific message ,for first it should be Illegal Value i.e default zkoss error and when he/she enters a reserved character ,it should show that reserved code has been put.
But somehow the following code doesnt work :
field_code.setConstraint("/[a-zA-Z]/ : {Illegal Value} ,/[^AaRrUuFf]/ : Reserved Code");
The output is the first regex works fine but on offending the same " {Illegal Value} ,/[^AaRrUuFf]/ : Reserved Code" is displayed as error.
You can't do it in the zul, but with help of a SimpleConstraint you could create this.
Create your own class, and extend SimpleConstraint.
Then hold 2 Matcher vars for each constraint.
At last, override the Validate method to something like this :
#Override
public void validate(Component comp, Object value) {
if (value != null && value instanceof String) {
String stringValue = (String) value;
if (!expression1.reset(stringValue).matches()) {
throw new WrongValueException(comp, errorMsg1);
}
if (!expression2.reset(stringValue).matches()) {
throw new WrongValueException(comp,errorMsg2);
}
} else {
// do what needs to be done when value is null or not a String.
}
}

How to find matches that occur within a specified string with regex?

I have a unique situation where I need to query a mongo database to find the names of people who occur in a body of text. The query must specify the body of text and find records with values that occur in the body of text. How can I do this with a regular expression?
I need to write a query where this would match:
/Jonathan is a handsome guy/.test('Jonathan')
The problem is that the text inside "test" is the value of a mongo field, so this query must be written such that the body of text is provided as input, and it matches on names that occur within (are substrings of) the body of text.
A more concrete example:
db.test.find();
{ "_id" : ObjectId("547e9b79f2b519cd1657b21e"), "name" : "Jonathan" }
{ "_id" : ObjectId("547e9b88f2b519cd1657b21f"), "name" : "Sandy" }
db.test.find({name: { $in: [/Jonathan has the best queries/]} } );
I need to construct a query that would return "Jonathan" when provided the input "Jonathan has the best queries"
This $where may do the trick, though can be very slow:
db.test.find({$where: function() {
var mystr = '/Jonathan has the best queries/';
var patt = new RegExp(this.name);
if (patt.test(mystr)) return true;
return false;
}})

How to check whether given email address is invalid in action script 3?

I need to check whether given email address is invalid in action script. Following is the code/regex i came up with.
private function isEmailInvalid(email:String):Boolean
{
var pattern:RegExp = /(\w|[_.\-])+#((\w|-)+\.)+\w{2,4}+/;
var result:Object = pattern.exec(email);
if(result == null) {
return true;
}
return false;
}
But it seems like above code do not cover all the test cases in the following link:
http://blogs.msdn.com/b/testing123/archive/2009/02/05/email-address-test-cases.aspx
Does anyone have better way of doing this?
Folowing are the tested valid emails i used (above function should return "false" for these):
firstname.lastname#domain.com
firstname+lastname#domain.com
email#domain.co.jp
Folowing are the invalid ones (so function should return "true" for these):
email#domain#domain.com
.email#domain.com
email..email#domain.com
plainaddress, email#domain..com
Remove the + at the last and you must need to put anchors.
^(\w|[_.\-])+#((\w|-)+\.)+\w{2,4}$
Simplified one,
^[\w_.-]+#([\w-]+\.)+\w{2,4}$
DEMO
Try this RegExp :
RegExp = /\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*/;

How do I mutate a tritium variable string?

I've fetched the entire list of classes from the body into a variable like so:
$page_code = fetch("self::body/#class")
But I only want to grab one class out. I tried using replace() and a regex on the variable contents but I think something's up with my syntax:
$page_code {
text() {
replace(/[^A-Z]*/, '')
}
log("############## page code is " + $page_code)
}
I think the code you want is:
$page_code {
replace(/[^A-Z]*/, '')
}
log("############## page code is " + $page_code)
Since $page_code is already a string, you don't need to open up a text() scope. Also, the log() statement should be outside, or else it will just log the current value of $page_scope.
See it here: http://tester.tritium.io/8ccb7ef99a0fd2fcbd60bd74cc137b040f57555e
$page_code is already text() so opening a text scope does nothing.
View this example here:
http://tester.tritium.io/3a9f3bfcffe41ab39a9f6927965d5b173692485f
Code:
html() {
$("/html") {
$page_code = fetch("./body/#class")
log("############## page code is " + $page_code)
$page_code {
replace(/[^A-Z]/, '')
}
log("############## page code is " + $page_code)
# better way to do this
$("./body") {
attribute("class") {
value() {
replace(/[^A-Z]/, '')
}
}
}
}
}
The second way edits the body class and sets the new body class for you.

Magento: Get product id in php block for available in list.phtml

I'm trying to get an attribute for it to be listed on list.phtml, the form that is being made is as follows:
I created a module on the Block and created a function which captures the attribute:
protected function getPreOrder()
{
$productId = $this->getRequest()->getParam('id');
$product = Mage::getModel('catalog/product')->load($productId);
$preOrder = $product->getNewsFromDate();
$preOrder = substr($preOrder, 0, 10);
return $preOrder;
}
public function getViewList()
{
if(strtotime(date('Y-m-d')) <= strtotime($this->getPreOrder()))
{
return true;
} else {
return false;
}
}
However, nothing is returned. I also did this same method to view.phtml and it worked perfectly. That goes for a file before the function getChildHtml() phtml, is not being edited list.phtml
That makes sense to create a loop, but the loop is already list.phtml!
What would be the way?
I thank you.
Have you debugged your block function to see if the product id is correct and if its loading the model correctly ??
Also debug the template list.phtml to check if its correctly loading the block type ?
get_class($this);
and see what class type is it.