CouchDB get all Special Characters - view

I need to get all the special characters from a view.
I have documents like this:
doc.name: ""
doc.name: " "
doc.name: "+Name"
doc.name: "*Name"
I try this params:
startkey: 'null', endkey: '#'
and
startkey: '\u0000', endkey: '\u0040'
But I can't get the contacts starting with '+' or '*'.
This is my view:
"function(doc) { if (doc.name) { emit(doc.name.charAt(0).toLowerCase(), doc) }}"
My final idea is get all characters not beginning with letters.
How many request I need to do for this and which params should I use?

Ok, I find the way to do this, first to get all special characters I do:
startkey: '', endkey: '9'
But like this I don't get the doc when the key don't exist or is null, because my view, so I change my view to:
"function(doc) { if (doc) { if (doc.name)
{ emit(doc.name.charAt(0).toLowerCase(), doc) } else { emit('', doc) }
} }"
And like this I can get everything.
I don't know if this is the best way to do this, but it's working.

Related

Yup validation with one field but two possible options (OR)

I am trying to create an input that takes a string that will be used as the href value for a tag. The href can be a url OR an email (for mailto:).
It works if I just check for email, or if I just check for URL. However, I want to check for one or the other. I am looking through yup documentation but I can't find a way to do an OR.
I noticed that there is a when to test for another field but I'm not checking if another field is true or not, or use test but I also can't seem to get it to work.
const vSchema = yup.object().shape({
text: yup.string().required(),
href: yup
.string()
.email('Link must be a URL or email')
.url('Link must be a URL or email')
.required('Link is a required field'),
});
test this
yup.addMethod(yup.string, "or", function(schemas, msg) {
return this.test({
name: "or",
message: "Please enter valid url or email." || msg,
test: value => {
if (Array.isArray(schemas) && schemas.length > 1) {
const resee = schemas.map(schema => schema.isValidSync(value));
return resee.some(res => res);
} else {
throw new TypeError("Schemas is not correct array schema");
}
},
exclusive: false
});
});

Alter the text displayed for a token in ace-editor

I have this example where I am locating a matching string via regex and changing the styles using highlight rules.
this.$rules = {
start: [{
token: 'variableRef',
regex: /\$variable\..+\$/
}]
};
and alter the color using a css class:
.ace_variableRef {
color: red;
}
But what I would really like to do is change the text that is being displayed from $variable.1.name$ to the "resolved value". I have access to:
var variables = {
1: 'timeout'
};
so I can use the reference path to get the value, but is it even possible to do this with ace-editor?
Ideally I would display the string in the user friendly way, but keep the original reference value handy (in metadata or something) since that is what is actually stored in the db.
You can accomplish this by defining a custom onMatch for your rule, like so
this.$rules = {
start: [{
onMatch: function(value, state, stack) {
var values = this.splitRegex.exec(value);
return [{
type: 'variableRef',
value: variables[values[1]]
}]
},
regex: /\$variable\.(\d+).+\$/
}]
};
but the actual text will remain unaltered (thus causing oddities with text selection/cursor), so you'll need to pad/clip the resulting value for it to match length of values[0]

Using SyntaxHighlightRules for Syntax validation

I've built some new Ace Editor Modes for my custom language (JMS Message representations) with a sophisticated state machine. Now it would be great to reuse that syntax highlighting also to create the errors. Is that possible ?
In other words, let's say my syntax highlighting creates 'invalid' tokens and I want to use the line number of that token to flag an error and then do something like this: https://github.com/ajaxorg/ace/wiki/Syntax-validation
The simplest format is the HEX format:
this.$rules = {
"start": [
{ regex: /[!#].*$/, token: "comment" },
{ regex: /^0x[0-9a-f]+:/, token: "constant" }, // hex offset
{ regex: /(?:[0-9a-fA-F]{4} |[0-9a-fA-F]{2} )/, token: "constant.numeric" }, // hex value
{ regex: /[\S ]{1,16}$/, token: "string" }, // printable value
{ regex: "\\s+", token: "text" },
{ defaultToken: "invalid" }
]
};
And let's say the editor created this state with an invalid token in line 4:
Is there a (preferably easy) way to get to the line numbers of my invalid tokens ? Or to reuse my $rules state machine for syntax checking ?
Found it - I must admin, Ace Editor is really good stuff. Always works as expected.
What works for me, after computing the tokens of the document with the rules state machine, I iterate through all tokens and find the once that are 'invalid' and then set annotations on those lines. Initially simply 'Syntax error' but different types of 'invalid' could mean different things in the future. This way I only have to write the validation syntax validation once.
aceEditor.session.on('change', function(delta) {
var sess = aceEditor.session;
sess.clearAnnotations();
var invalids = [];
for( var row=0;row<sess.getLength();row++ ) {
var tokens = sess.getTokens(row);
if( !tokens ) continue;
for( var t=0;t<tokens.length;t++ ) {
if( tokens[t].type==="invalid" ) {
invalids.push({ row: row, column: 0, text: "Syntax error", type: "error" });
}
}
}
sess.setAnnotations( invalids );
});
There might be a smarter way to do this (maybe an onToken(type,row,column) function somewhere ?), but above works for me.

bootstrap-typeahead's displayfield using multiple values

I'm sorry if this is a duplicate question but I do not understand the answers of other people. I'm using Twitter Bootstrap Ajax Typeahead Plugin (https://github.com/biggora/bootstrap-ajax-typeahead/) to search emails from data that comes from an SQL query. This is the code I use with a php file, where I use people's emails as valueField and people's names as displayField and it works well.
inputSearch.typeahead({
ajax: {
url: urlAjax + '?requete=rechercheannuaire',
displayField: "description",
valueField: "id",
triggerLength: 2,
method: "get",
loadingClass: "loading-circle",
preProcess: function(data){
if(data.type === "error")
{
return false;
}
return data.datas;
}
},
onSelect: function(data){
//alert("assez tot");
data.text = data.value;
//console.log(data);
$("#chercherinvite").val(data.text);
return data;
}
});
The problem is that I have to be able to search "Dujardin" as well as "Du Jardin" and I cannot find a way to assign multiple values to displayField. If someone could explain how typeahead works, I'd be thankfull, I don't understand the documentation.
According to the plugin documentation, you cannot assign multiple values to the displayField option. However, it is possible for you to re-write events.
After a quick lookup into the source code of bootstrap-ajax-typeahead, we can figure out that the "matcher" event is used as the filter for displaying - or not - values to the user.
To allow to match both "Du jardin" and "Dujardin", we have to manipulate strings. Here, I suggest you to :
Remove any diacritic character
Remove any non-word character (all except [A-Za-z0-9_])
Remove any underscore
Set the string to lowercase
To do #1, I suggest you to use this fantastic script by rdllopes.
I wrote a POC. Here is the JSON source (called "source.json"):
[
{ "id": 1, "name": "jdupont#example.com - Jean Du Pont"},
{ "id": 2, "name": "jdupont2#example.com - Jean Dupont"},
{ "id": 3, "name": "jdupont3#example.com - Jéan Dupônt"},
{ "id": 4, "name": "mbridge#example.com - Michel Bridge"}
]
And here is the script that I used for matching elements :
$('#search').typeahead({
// Our source is a simple JSON file
ajax: 'source.json',
// Display field is a list of names
displayField: 'name',
// And value field a list of IDs
valueField: 'id',
matcher: function(item)
{
// For both needle and haystack, we :
// 1. Remove any diacritic character
// 2. Remove any non-word character (all except [A-Za-z0-9_])
// 3. Remove any underscore
// 4. Set the string to lowercase
var needle = removeDiacritics(this.query).replace(/[^\w]/gi, '').replace('_', '').toLowerCase();
var haystack = removeDiacritics(item).replace(/[^\w]/gi, '').replace('_', '').toLowerCase();
// Does the needle exists in haystack?
return ~haystack.indexOf(needle);
}
});

CKEditor - Use Advanced Content Filter rule to specify values

It seems that some plugins of CKEditor specify values of properties. For example, the left-to-right plugin has the following rule:
{
"styles":null,
"requiredStyles":null,
"classes":null,
"requiredClasses":null,
"attributes":{
"dir":"ltr"
},
"requiredAttributes":{
"dir":true
},
"elements":{
"span":true
},
"featureName":"styles",
"propertiesOnly":false,
"match":null
},
How can I specify values with string format rules?
Something like span[!dir=ltr].
You can't. String format doesn't allow such definition. You can specify span[!dir] so all spans require dir attribute and nothing else. With object definition you can do more, e.g. use functions:
...
'ul, li: true,
'$0': {
match: function( el ) {
return el.name == 'b';
},
propertiesOnly: true,
attributes: 'dir'
}
'$1': {
...
Why do you persist to use string format? You can use objects and store it as JSON.

Resources