1
0
Fork 0

props.nas: add isValidPropName and makeValidPropName; add props.nut

This commit is contained in:
Henning Stahlke 2020-06-05 18:16:48 +02:00 committed by James Turner
parent 47a5ffe3d1
commit ed1deacce8
2 changed files with 74 additions and 1 deletions

View file

@ -112,7 +112,37 @@ var Node = {
me.getPath()~" "~me.getType());
return 0;
}
},
# checks if a string can be used as a prop name
# returns 0 (invalid) or 1 (valid)
# as string operations are expensive, use this only when necessary,
# especially do not use in update loops
isValidPropName: func(s) {
if (!size(s)) return 0;
var _ispropspecial = func(c) { c == `_` or c == `.` or c == `-`; }
var _ispropname = func(c) string.isalnum(c) or _ispropspecial(c);
if (!string.isalpha(s[0]) and s[0] != `_`) return 0;
for (var i=1; i < size(s); i += 1) {
if (!_ispropname(s[i])) return 0;
}
return 1;
},
# replaces any invalid char by "_"
# as string operations are expensive, use this only when necessary,
# especially do not use in update loops
makeValidPropName: func(s) {
if (!size(s)) return nil;
var _ispropspecial = func(c) { c == `_` or c == `.` or c == `-`; }
var _ispropname = func(c) string.isalnum(c) or _ispropspecial(c);
var rv = "";
rv ~= (!string.isalpha(s[0])) ? "_" : chr(s[0]);
for (var i=1; i < size(s); i += 1) {
rv ~= (_ispropname(s[i])) ? chr(s[i]) : "_";
}
return rv;
},
};
##

43
Nasal/props.nut Normal file
View file

@ -0,0 +1,43 @@
#-------------------------------------------------------------------------------
# SPDX-License-Identifier: GPL-2.0-or-later
#-------------------------------------------------------------------------------
# props.Node.nut - Nasal Unit Test for props.Node.nas
# created: 06/2020
# Copyright (C) 2020 by Henning Stahlke
#-------------------------------------------------------------------------------
var setUp = func {
print("setUp "~caller(0)[2]~" ");
};
var tearDown = func {
};
var test_isValidPropName = func() {
# test valid names
unitTest.assert(props.Node.isValidPropName("abc123") == 1, "isValidPropName()");
unitTest.assert(props.Node.isValidPropName("_abc123") == 1, "isValidPropName()");
unitTest.assert(props.Node.isValidPropName("_1a") == 1, "isValidPropName()");
unitTest.assert(props.Node.isValidPropName("_1a.-") == 1, "isValidPropName()");
# test invalid names
unitTest.assert(props.Node.isValidPropName("1a") == 0, "isValidPropName()");
unitTest.assert(props.Node.isValidPropName("abä") == 0, "isValidPropName()");
unitTest.assert(props.Node.isValidPropName("foo:bar") == 0, "isValidPropName()");
unitTest.assert(props.Node.isValidPropName("foo<bar") == 0, "isValidPropName()");
unitTest.assert(props.Node.isValidPropName("") == 0, "isValidPropName()");
}
var test_makeValidPropName = func() {
# test valid names
unitTest.assert(props.Node.makeValidPropName("abc123") == "abc123", "makeValidPropName()");
unitTest.assert(props.Node.makeValidPropName("_abc123") == "_abc123", "makeValidPropName()");
unitTest.assert(props.Node.makeValidPropName("_1a") == "_1a", "makeValidPropName()");
unitTest.assert(props.Node.makeValidPropName("_1a.-") == "_1a.-", "makeValidPropName()");
# test invalid names
unitTest.assert(props.Node.makeValidPropName("1a") == "_a", "makeValidPropName()");
unitTest.assert(props.Node.makeValidPropName("foo:bar") == "foo_bar", "makeValidPropName()");
unitTest.assert(props.Node.makeValidPropName("foo<bar") == "foo_bar", "makeValidPropName()");
unitTest.assert(props.Node.makeValidPropName("") == nil, "makeValidPropName()");
unitTest.assert(props.Node.makeValidPropName("abä") == "ab_", "makeValidPropName()");
}