Parse() Version Number with a groovy script? - javascript

First time posting here.
I would like to ask if there's a way to parse a version number using a groovy script.
I extract from Ariba a payload, the issue comes with a specific field called ItemNumber. At first it was working, but this month I started to retrieve a version instead of a float.
This is the part of the script that needs to be changed, but I can't find a way to do it.
if (ItemNumber?.trim()){
list.ItemNumber = Double.parseDouble(ItemNumber.toString());
}
Any help is greatly appreciated,
Thank you,
Kostas

To parse a version number, you will need to tokenize the string. You should create a Version class to hold the version information. This can help when sorting a list of versions. After tokenizing, you can call the Version constructor, and pass the tokens as integers.
Once you have a Version object, you can access fields like major, minor, and patch.
class Version {
int major
Integer minor, patch
#Override String toString() {
return [major, minor, patch].findAll().join('.')
}
}
def parseVersion(String versionString) {
if (!versionString) return null
int[] tokens = versionString.split(/\./).collect { it as int }
return new Version(
major: tokens[0],
minor: tokens.length > 1 ? tokens[1] : null,
patch: tokens.length > 2 ? tokens[2] : null,
)
}
class Payload {
String ItemNumber
}
Payload payload = new Payload(ItemNumber: "2.4")
Version version = parseVersion(payload.ItemNumber?.trim())
printf("Major version : %d%n", version.major)
printf("Minor version : %s%n", version.minor ?: "<UNSET>")
printf("Patch version : %s%n", version.patch ?: "<UNSET>")
printf("Full version : %s%n", version)
In later versions of Groovy, you can call the Versions constructor like this:
new Version(major: tokens[0], minor: tokens?[1], patch: tokens?[2])

Related

Parsing a string to true Integer in Javascript

I got a question about parsing a string to a true integer in javascript. I have a back-end method I've written that accepts an accountNumber (string) and a year (int), and I am calling into this method from the front-end.
The method complains unless it's being passed an actual int, and not a 'number'.
I've tried parsing my string a few ways:
const year = parseInt(stringValue);
typeof year == number
const year = Number.parseInt(stringValue);
Number.isInteger(year) == false
Number.isInteger(2018) == true
I even tried using the Math class, but with no luck.
const year = Math.floor(stringValue);
Where the year is still just a 'number'
In addition, I tried hard coding 2018 into my function and it worked as expected. I just can't seem to parse my value to a true integer and am wondering, how can I go about this? Or is it just recommended that I accept a string on the back-end and parse it there?
====== EDIT =======
I am trying it with AngularJS.
So I'm using angularJs and I got an with ng-model="account.searchDocsByYear", and I use the $scope value of account.searchDocsByYear as the argument for parsing. It still returns false. Not sure what is different. All the mentioned examples check out fine for me too. But console.log(Number.isInteger(Number.parseInt($scope.account.searchDocsByYear))); still returns false
Thanks!
You could use the Unary plus(+) for this purpose which is less verbose and attempts to convert its operand into a number, like this:
const stringValue = '2018';
const year = +stringValue;
console.log('Is integer: ' + Number.isInteger(year))
Working as expected.
See below snippet:
function ctrl($scope){
$scope.account = {
"searchDocsByYear" : "2018"
}
console.log(Number.isInteger(Number.parseInt($scope.account.searchDocsByYear)))
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<table ng-app="" ng-controller=ctrl>
</table>

node.js change in concatenation?

I'm trying to debug some code that another programmer has left for me to maintain. I've just attempted to upgrade from node.js 5 to node.js 8 and my database queries are for some requests coming back with key not found errors
We're using couchbase for the database and our document keys are "encrypted" for security. So we may have a key that starts like this "User_myemail#gmail.com" but we encrypt it using the following method:
function _GetScrambledKey(dbKey)
{
//select encryption key based on db key content
var eKeyIndex = CalculateEncryptionKeyIndex(dbKey, eKeys.length);
var sha = CalculateSHA512(dbKey + eKeyIndex);
return sha;
}
function CalculateEncryptionKeyIndex(str, max)
{
var hashBuf = CalculateSHA1(str);
var count = 0;
for (var i = 0; i < hashBuf.length; i++)
{
count += hashBuf[i];
count = count % max;
}
return count;
}
We then query couchbase for the document with
cb.get("ECB_"+encryptedKey, opts, callback);
In node5 this worked but in node8 we're getting some documents return fine and others return as missing. I outputted the "ECB_"+encryptedKey as an int array and the results have only confused me more. They are different on node5 to node8 but only by one character right in the middle of the array.
Outputting the encryptedKey as an int array on both versions shows this
188,106,14,227,211,70,94,97,63,130,78,246,155,65,6,148,62,215,47,230,211,109,35,99,21,60,178,74,195,13,233,253,187,142,213,213,104,58,168,60,225,148,25,101,155,91,122,77,2,99,102,235,26,71,157,99,6,47,162,152,58,181,21,175
Then outputting the concatenated string, in the same way, shows slightly different results
This is the node8 output
Node8 key: 69,67,66,95,65533,106,14,65533,65533,70,94,97,63,65533,78,65533,65533,65,6,65533,62,65533,47,65533,65533,109,35,99,21,60,65533,74,65533,13,65533,65533,65533,65533,65533,65533,104,58,65533,60,65533,25,101,65533,91,122,77,2,99,102,65533,26,71,65533,99,6,47,65533,65533,58,65533,21,65533
And this is the node5 output
Node5 key: 69,67,66,95,65533,106,14,65533,65533,70,94,97,63,65533,78,65533,65533,65,6,65533,62,65533,47,65533,65533,109,35,99,21,60,65533,74,65533,13,65533,65533,65533,65533,65533,65533,104,58,65533,60,65533,65533,25,101,65533,91,122,77,2,99,102,65533,26,71,65533,99,6,47,65533,65533,58,65533,21,65533
I had to run it through a diff tool to see the difference
Comparing that to the original pre-append array it looks like the 225 has just been dropped in node8. Is 225 significant? I can't understand how that would be possible otherwise unless it's a bug. Does anyone have any ideas?
Looks like this was a change in v8 5.5 https://github.com/nodejs/node/issues/21278
A lot of the issues you are facing, including the concatenation can be cleaned up using newer features from ES6 that are available in node 8.
In general, you should avoid doing string concatenations with the + operator and should use string literals instead. In your case, you should replace the "ECB_"+encryptedKey with `ECB_${encryptedKey}`.
Additionally, if you want to output the contents of the integers values from this concatenated string, then you are better off using .join, the spread operator (...) and the Buffer class from Node as follows:
let encKey = `ECB_${encryptedKey}`;
let tmpBuff = Buffer.from(encKey);
let buffArrVals = [...tmpBuff];
console.log(buffArrVals.join(','));
Also, if you can help it, you really should avoid using var inside of function blocks like it exists in your sample code. var performs something called variable hoisting and causes the variable to become available outside the scope it was declared, which is seldom intended. From node 6+ onward the recommendation is to use let or const for variable declarations to ensure they stay scoped to the block they are declared.

Mapping DataTables migration 1.9 to 1.10

Hello StackOverflow community,
I'm looking for some help to migrate from Datatables 1.9 to 1.10. The notation is not the same anymore, so in order to keep the "old" code working with the new Datatables version, I need to map them. I wrote a json object "mapping", like that (it's just an extract), based on this link https://datatables.net/upgrade/1.10-convert :
var mapping : {"oLanguage" :  {"sEmptyTable" : "language.emptyTable"}};
Now, I would like to create a JSON object from "language.emptyTable" to :
{"language" : {"emptyTable"}}
I tried some different recursive functions but... call stack and so on.
I know it's not a very clean method to upgrade to new version but I have at least a hundred Datatables in my app and can't modify all this code in time, I will do it step by step and not in a hurry.
Thank you very much for your help.
I was working on it since yesterday and found the answer just after I posted this message... So, if someone is interested by the answer, here it is:
<script>
var txt = 'language.search';
var fn = function(str) {
var obj = {};
var pos = str.indexOf('.');
//console.log(pos);
if(pos != '-1') {
//console.log(str.substr(pos+1));
obj[str.substr(0,pos)] = fn(str.substr(pos+1));
}
else if (str.length) {
obj[str] = {};
//return obj;
}
return obj;
};
console.log(fn(txt));
There is no need to convert options, jQuery DataTables 1.10 has backward support of options used by jQuery Datatables 1.9.
See Converting parameter names for 1.10:
The change is fully backwards compatible and you can continue to use the old versions of the parameter names and API methods as you were before.

MongoDB checking for multiple regex matches inside a list for free text search

I am setting up a mongoDB db to allow (simple) keyword searching using multikeys as recommended here. A record looks similar too:
{ title: { title: "A river runs through", _keywords: ["a","river","runs","through"] ) , ... }
I using nodejs server side, so am using javascript. The following query will match (this was run in the mongo terminal):
> db.torrents_sorted.find({'title._keywords' : {"$all" : ["river","the"]} }).count()
210
However, these do not:
> db.torrents_sorted.find({'title._keywords' : {"$all" : ["/river/i","/the/i"]} }).count()
0
> db.torrents_sorted.find({'title._keywords' : {"$all" : [{ "$regex" : "river", "$options" : "i" },{ "$regex" : "the", "$options" : "i" }]} }).count()
0
Using a single regex (without using $and or $all) does match:
db.torrents_sorted.find({'title._keywords' : { "$regex" : "river", "$options" : "i" } }).count()
1461
Interestingly, using python and pymongo to compile the regular expressions does work:
>>> db.torrents_sorted.find({'title._keywords': { '$all': [re.compile('river'), re.compile('the')]}}).count();
236
I am not necessarily looking for a solution that uses regexes, however it is required that keywords are matched on shorter strings so "riv" matches "river", which seems ideal for regexes (or LIKE in sql).
My next idea is to try passing in a javascript function that performs the regex matching on the list, or perhaps passing in a seperate function for each regex (this does seem to scream hack at me :), although I'm guessing this would be slower and performance is very important.
You might want to use the $and operator.
Ok, I have an answer, that is kinda interesting in a different way. The bug I was experiencing with regexes exists in version 1.8 of mongodb and has been solved, it is shown here.
Sadly the hosting company looking after the db atm are not able to offer version 2.0, and the $and keyword was added in version 2.0, although thanks for the debug help Samarth.
So instead I have written a javascript function to perform the regex matching:
function () {
var rs = [RegExp(".*river.*"), RegExp(".*runs.*")];
for(var j = 0; j < rs.length; j++) {
var val = false;
for (var i = 0; !val && i < this.title._keywords.length; i++)
val = rs[j].test(this.title._keywords[i]);
if(!val) return false;
}
return true;
}
This runs in O(n^2) time (not very cool), but will fail in linear time, if the first regex does not match on any on the keywords (since I am looking for a disjunction).
Any input on optimising this would be greatly appreciated, although if this is the best solution I can find for 1.8, I may have to find somewhere else to store my db in the near future, ;).

How to test valid UUID/GUID?

How to check if variable contains valid UUID/GUID identifier?
I'm currently interested only in validating types 1 and 4, but it should not be a limitation to your answers.
Currently, UUID's are as specified in RFC4122. An often neglected edge case is the NIL UUID, noted here. The following regex takes this into account and will return a match for a NIL UUID. See below for a UUID which only accepts non-NIL UUIDs. Both of these solutions are for versions 1 to 5 (see the first character of the third block).
Therefore to validate a UUID...
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i
...ensures you have a canonically formatted UUID that is Version 1 through 5 and is the appropriate Variant as per RFC4122.
NOTE: Braces { and } are not canonical. They are an artifact of some systems and usages.
Easy to modify the above regex to meet the requirements of the original question.
HINT: regex group/captures
To avoid matching NIL UUID:
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
If you want to check or validate a specific UUID version, here are the corresponding regexes.
Note that the only difference is the version number, which is explained in 4.1.3. Version chapter of UUID 4122 RFC.
The version number is the first character of the third group : [VERSION_NUMBER][0-9A-F]{3} :
UUID v1 :
/^[0-9A-F]{8}-[0-9A-F]{4}-[1][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
UUID v2 :
/^[0-9A-F]{8}-[0-9A-F]{4}-[2][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
UUID v3 :
/^[0-9A-F]{8}-[0-9A-F]{4}-[3][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
UUID v4 :
/^[0-9A-F]{8}-[0-9A-F]{4}-[4][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
UUID v5 :
/^[0-9A-F]{8}-[0-9A-F]{4}-[5][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
regex to the rescue
/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.test('01234567-9ABC-DEF0-1234-56789ABCDEF0');
or with brackets
/^\{?[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}‌​\}?$/
If you are using Node.js for development, it is recommended to use a package called Validator. It includes all the regexes required to validate different versions of UUID's plus you get various other functions for validation.
Here is the npm link: Validator
var a = 'd3aa88e2-c754-41e0-8ba6-4198a34aa0a2'
v.isUUID(a)
true
v.isUUID('abc')
false
v.isNull(a)
false
If you use the uuid package, this package brings a boolean validation function where it tells you if a uuid is valid or not.
Example:
import { validate as isValidUUID } from 'uuid';
if (!isValidUUID(tx.originId)) {
return Promise.reject('Invalid OriginID');
}
thanks to #usertatha with some modification
function isUUID ( uuid ) {
let s = "" + uuid;
s = s.match('^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$');
if (s === null) {
return false;
}
return true;
}
Beside Gambol's answer that will do the job in nearly all cases, all answers given so far missed that the grouped formatting (8-4-4-4-12) is not mandatory to encode GUIDs in text. It's used extremely often but obviously also a plain chain of 32 hexadecimal digits can be valid.[1] regexenh:
/^[0-9a-f]{8}-?[0-9a-f]{4}-?[1-5][0-9a-f]{3}-?[89ab][0-9a-f]{3}-?[0-9a-f]{12}$/i
[1] The question is about checking variables, so we should include the user-unfriendly form as well.
Why are there dashes in a .NET GUID? - Stack Overflow plus Accepted answer
Test and validate a GUID (guid.us)
Guid.ToString Method (String) (MSDN)
All type-specific regexes posted so far are failing on the "type 0" Nil UUID, defined in 4.1.7 of the RFC as:
The nil UUID is special form of UUID that is specified to have all 128 bits set to zero: 00000000-0000-0000-0000-000000000000
To modify Wolf's answer:
/^[0-9a-f]{8}-?[0-9a-f]{4}-?[0-5][0-9a-f]{3}-?[089ab][0-9a-f]{3}-?[0-9a-f]{12}$/i
Or, to properly exclude a "type 0" without all zeros, we have the following (thanks to Luke):
/^(?:[0-9a-f]{8}-?[0-9a-f]{4}-?[1-5][0-9a-f]{3}-?[89ab][0-9a‌​-f]{3}-?[0-9a-f]{12}‌​|00000000-0000-0000-‌​0000-000000000000)$/‌​i
if you use the uuid package, you can import the validate and pass the id into it
const { v4: uuidv4, validate } = require('uuid');
const { id } = request.params;
validate(id) ? true : false;
I think Gambol's answer is almost perfect, but it misinterprets the RFC 4122 § 4.1.1. Variant section a bit.
It covers Variant-1 UUIDs (10xx = 8..b), but does not cover Variant-0 (0xxx = 0..7) and Variant-2 (110x = c..d) variants which are reserved for backward compatibility, so they are technically valid UUIDs. Variant-4 (111x = e..f) is indeed reserved for future use, so they are not valid currently.
Also, 0 type is not valid, that "digit" is only allowed to be 0 if it's a NIL UUID (like mentioned in Evan's answer).
So I think the most accurate regex that complies with current RFC 4122 specification is (including hyphens):
/^([0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[0-9a-d][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i
^ ^^^^^^
(0 type is not valid) (only e..f variant digit is invalid currently)
A slightly modified version of the above answers written in a more concise way. This will validate any GUID with hyphens (however easily modified to make hyphens optional). This will also support upper and lower case characters which has become the convention regardless of the specification:
/^([0-9a-fA-F]{8})-(([0-9a-fA-F]{4}\-){3})([0-9a-fA-F]{12})$/i
The key here is the repeating part below
(([0-9a-fA-F]{4}\-){3})
Which simply repeats the 4 char patterns 3 times
If someone is using yup , JavaScript schema validator library, This functionality can be achieved with below code.
const schema = yup.object().shape({
uuid: yup.string().uuid()
});
const isValid = schema.isValidSync({uuid:"string"});
Use the .match() method to check whether String is UUID.
public boolean isUUID(String s){
return s.match("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$");
}
A good way to do it in Node is to use the ajv package (https://github.com/epoberezkin/ajv).
const Ajv = require('ajv');
const ajv = new Ajv({ allErrors: true, useDefaults: true, verbose: true });
const uuidSchema = { type: 'string', format: 'uuid' };
ajv.validate(uuidSchema, 'bogus'); // returns false
ajv.validate(uuidSchema, 'd42a8273-a4fe-4eb2-b4ee-c1fc57eb9865'); // returns true with v4 GUID
ajv.validate(uuidSchema, '892717ce-3bd8-11ea-b77f-2e728ce88125'); // returns true with a v1 GUID
Versions 1 to 5, without using a multi-version regex when version is omitted.
const uuid_patterns = {
1: /^[0-9A-F]{8}-[0-9A-F]{4}-1[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
2: /^[0-9A-F]{8}-[0-9A-F]{4}-2[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
3: /^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
4: /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
5: /^[0-9A-F]{8}-[0-9A-F]{4}-5[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
};
const isUUID = (input, version) => {
if(typeof input === "string"){
if(Object.keys(uuid_patterns).includes(typeof version === "string" ? version : String(version))){
return uuid_patterns[version].test(input);
} else {
return Object.values(uuid_patterns).some(pattern => pattern.test(input));
}
}
return false;
}
// Testing
let valid = [
'A987FBC9-4BED-3078-CF07-9141BA07C9F3',
'A987FBC9-4BED-4078-8F07-9141BA07C9F3',
'A987FBC9-4BED-5078-AF07-9141BA07C9F3',
];
let invalid = [
'',
'xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3',
'A987FBC9-4BED-3078-CF07-9141BA07C9F3xxx',
'A987FBC94BED3078CF079141BA07C9F3',
'934859',
'987FBC9-4BED-3078-CF07A-9141BA07C9F3',
'AAAAAAAA-1111-1111-AAAG-111111111111',
];
valid.forEach(test => console.log("Valid case, result: "+isUUID(test)));
invalid.forEach(test => console.log("Invalid case, result: "+isUUID(test)));
I added a UUID validator to Apache Commons Validator. It's not yet been merged, but you can vote for it here:
https://github.com/apache/commons-validator/pull/68
I have this function, but it's essentially the same as the accepted answer.
export default function isUuid(uuid: string, isNullable: boolean = false): boolean {
return isNullable
? /^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(uuid)
: /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(uuid);
}
I think a better way is using the static method fromString to avoid those regular expressions.
id = UUID.randomUUID();
UUID uuid = UUID.fromString(id.toString());
Assert.assertEquals(id.toString(), uuid.toString());
On the other hand
UUID uuidFalse = UUID.fromString("x");
throws java.lang.IllegalArgumentException: Invalid UUID string: x

Categories