@sap/odata-v4
Version:
OData V4.0 server library
107 lines (90 loc) • 3.76 kB
JavaScript
;
const validateThat = require('./validator/ParameterValidator').validateThat;
const IllegalArgumentError = require('./errors/IllegalArgumentError');
/**
* <a href="../ODataSpecification/odata-v4.0-errata03-os/complete/part3-csdl/odata-v4.0-errata03-os-part3-csdl-complete.html#_Toc453752676">
* OData CSDL # 17.3 QualifiedName
* </a>
*/
class FullQualifiedName {
/**
* @param {string} namespace - namespace or alias of the schema
* @param {string} name - name value
*/
constructor(namespace, name) {
validateThat('namespace', namespace).truthy().typeOf('string');
validateThat('name', name).truthy().typeOf('string');
/**
* @type {string}
* @private
*/
this.namespace = namespace;
/**
* @type {string}
* @private
*/
this.name = name;
}
/**
* @returns {string} string representation of the full qualified name
*/
toString() {
return this.namespace + '.' + this.name;
}
/**
* Returns string representation of the input parameter, represented either as a
* FullQualifiedName instance or as an object having string 'namespace' and 'name' properties.
*
* @param {FullQualifiedName | {namespace: string, name: string}} fullQualifiedName - full
* qualified name, represented either as a FullQualifiedName instance or as an object having
* string 'namespace' and 'name' properties
*
* @returns {?string} string representation of the full qualified name or null, if null
* was specified as an input
*/
static getFQNAsString(fullQualifiedName) {
if (!fullQualifiedName) {
return null;
}
return fullQualifiedName.namespace + '.' + fullQualifiedName.name;
}
/**
* Creates FullQualifiedName instance from the specified full qualified name string.
*
* @param {string} namespaceAndName - string, representing a full qualified name, i.e. having namespace and name
* separated by dot character
* @returns {FullQualifiedName}
*/
static createFromNameSpaceAndName(namespaceAndName) {
validateThat('namespaceAndName', namespaceAndName).truthy().typeOf('string');
const dotIdx = namespaceAndName.lastIndexOf('.');
if (dotIdx === -1 || dotIdx === 0 || dotIdx === namespaceAndName.length - 1) {
throw IllegalArgumentError.createForMalformedValueFormat('namespaceAndName', '<namespace>.<name>');
}
return new FullQualifiedName(
namespaceAndName.substring(0, dotIdx),
namespaceAndName.substring(dotIdx + 1));
}
/**
* Compares two full qualified names.
*
* @param { FullQualifiedName | {namespace: string, name: string} } fullQualifiedName1 - first full qualified name
* @param { FullQualifiedName | {namespace: string, name: string} } fullQualifiedName2 - second full qualified name
* @returns {boolean} true if namespace and name of the first full qualified name strict equal to the namespace and
* name of the second full qualified name, otherwise - false
*/
static equals(fullQualifiedName1, fullQualifiedName2) {
if (typeof fullQualifiedName1 !== 'object' || typeof fullQualifiedName2 !== 'object') {
return false;
}
if (!fullQualifiedName1 && !fullQualifiedName2) {
return true;
}
if (!fullQualifiedName1 || !fullQualifiedName2) {
return false;
}
return (fullQualifiedName1.namespace === fullQualifiedName2.namespace) &&
(fullQualifiedName1.name === fullQualifiedName2.name);
}
}
module.exports = FullQualifiedName;