Examples

Every value you read from a πŸ“• queryDef result or a πŸ“• DOMElement is an untyped string, and before you act on one you often need to know what you are holding: is it present at all, a single value or a list? JavaScript’s built-in checks are unreliable on an AC server: typeof reports "object" for both arrays and null, and the !value shortcut calls 0, false, and "" empty β€” yet misses an empty array, since ![] is false.

NL.isEmpty, NL.isNundef, NL.isString, NL.isNumber, NL.isArray, and NL.isDate are Campaign’s type guards, written for exactly this input. isEmpty follows the XTK rule β€” null/undefined, or anything whose toString() is "", so "" and [] count as empty while 0 and false do not. Reach for them to test a value before you cast it with πŸ“• NL.XTK.parse.

isEmpty

NL.isEmpty returns true if the variable is null, or an empty array, or an empty string.

// truthy
logInfo(NL.isEmpty(""));
logInfo(NL.isEmpty(null));
logInfo(NL.isEmpty([]));
// falsy
logInfo(NL.isEmpty("0"));
logInfo(NL.isEmpty({}));
logInfo(NL.isEmpty(" "));
logInfo(NL.isEmpty([0]));
logInfo(NL.isEmpty(0));
logInfo(NL.isEmpty(false));

isXML

NL.isXML returns true if the variable is a deprecated E4X XML. Migrate it to a DOM XML.

// E4X source (deprecated)
const myXmlSource = <abc>def</abc>;
logInfo(NL.isXML(myXmlSource)); // true
// E4X class (deprecated)
const myXmlClass = new XML("<abc>def</abc>");
logInfo(NL.isXML(myXmlClass)); // true
// Entity
const myEntity = NLWS.nmsRecipient.create({ email: "[email protected]" });
logInfo(NL.isXML(myEntity)); // false

Unit tests

Tested with acc by running acc instance exec -f test/integration/xtk.is.spec.js in a sandbox:

loadLibrary("/nl/core/shared/nl.js"); // import NL namespace
NL.require("/nl/core/shared/xtk.js"); // import NL.XTK namespace
// isEmpty
context.isEmpty.res1 = NL.assert(NL.isEmpty("") === true);
context.isEmpty.res2 = NL.assert(NL.isEmpty(null) === true);
context.isEmpty.res3 = NL.assert(NL.isEmpty([]) === true);
context.isEmpty.res4 = NL.assert(NL.isEmpty("0") === false);
context.isEmpty.res5 = NL.assert(NL.isEmpty({}) === false);
context.isEmpty.res6 = NL.assert(NL.isEmpty(" ") === false);
context.isEmpty.res7 = NL.assert(NL.isEmpty([0]) === false);
context.isEmpty.res8 = NL.assert(NL.isEmpty(0) === false);
context.isEmpty.res9 = NL.assert(NL.isEmpty(false) === false);
// isXML
const myXmlSource = <abc>def</abc>; // E4X source
context.isXML.res1 = NL.assert(NL.isXML(myXmlSource) === true);
context.isXML.res2 = NL.assert(myXmlSource instanceof XML === true);
const myXmlClass = new XML("<abc>def</abc>"); // E4X class
context.isXML.res3 = NL.assert(NL.isXML(myXmlClass) === true);
context.isXML.res4 = NL.assert(myXmlClass instanceof XML === true);
const myEntity = NLWS.nmsRecipient.create({ email: "[email protected]" }); // Entity
context.isXML.res5 = NL.assert(NL.isXML(myEntity) === false);
context.isXML.res6 = NL.assert(myEntity instanceof XML === false);
// isLeapYear
context.isLeapYear.res1 = NL.assert(NL.XTK.isLeapYear(2000) === true);
context.isLeapYear.res2 = NL.assert(NL.XTK.isLeapYear(2001) === false);
context.isLeapYear.res3 = NL.assert(NL.XTK.isLeapYear(2002) === false);
context.isLeapYear.res4 = NL.assert(NL.XTK.isLeapYear(2003) === false);
context.isLeapYear.res5 = NL.assert(NL.XTK.isLeapYear(2004) === true);

See also

  • πŸ“• queryDef - Helper to run a SQL SELECT query with JSON and returns an XML element.
  • πŸ“• DOMElement - Adobe Campaign's XML element: the type returned by queryDef and SOAP calls. Can be read and traversed.