api-console-assets
Version:
This repo only exists to publish api console components to npm
130 lines (128 loc) • 3.75 kB
HTML
<!--
@license
Copyright 2016 The Advanced REST client authors <arc@mulesoft.com>
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy of
the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../arc-polyfills/arc-polyfills.html">
<script>
/**
* Common properties and methods for all RAML elements.
*
* @polymerBehavior Polymer.RamlBehavior
*/
Polymer.RamlBehavior = {
properties: {
// The RAML definition as a JavaScript object.
raml: Object,
// RAML base types
baseTypes: {
type: Array,
value: function() {
return ['object', 'array','integer', 'string','number','boolean',
'datetime','file', 'null','any'];
}
}
},
/**
* Returns true if the `obj` is empty.
* **Deprecated** Use `hasValue` instead.
*
* @param {any} obj Object to test for value.
* @return {Boolean}
*/
isEmpty: function(obj) {
var type = typeof obj;
if (!obj && ['number', 'boolean'].indexOf(type) === -1) {
return true;
}
if ((obj instanceof Array) && !obj.length) {
return true;
}
if ((obj instanceof Object) && !Object.keys(obj).length) {
return true;
}
return false;
},
/**
* Transforms type's properties to an array.
*
* Each array item object contsains a `key` property which is an Object's
* key.
*
* @param {Object} type RAML's type definition.
* @return {Object|undefined} The same object with properties as array.
*/
typeProperties: function(type) {
if (!type) {
return;
}
if (type.properties) {
if (!(type.properties instanceof Array)) {
var names = Object.keys(type.properties);
// https://github.com/mulesoft/api-console/issues/493
// Maps do not guarantee order. As the spec says:
// [object is] unordered collection of properties.
// This will "quasi" order the items, thoug original order set in
// RAML will never be preserved since output of a properties list
// is object.
names.sort();
var properties = [];
names.forEach(function(name) {
var item = type.properties[name];
item.key = name;
properties.push(item);
});
type.properties = properties;
}
}
return type;
},
/**
* Checks if passed object is set and has any value.
* 0 (zero) and empty string returns true.
*
* @return {Boolean} True/False depending if passed object has a value.
*/
hasValue: function(obj) {
var type = typeof obj;
if (type === 'number' && obj === 0) {
return true;
}
if (type === 'boolean') {
return true;
}
if (type === 'string' && obj === '') {
return true;
}
if ((obj instanceof Array) && !obj.length) {
return false;
}
if ((obj instanceof Object) && !Object.keys(obj).length) {
return false;
}
return !!obj;
},
/**
* Computes RAML type's object display name.
*
* @param {Object} type RAML type definition.
* @return {String|undefined} (in that order) display name, name, key and
* `undefined`
*/
typeName: function(type) {
if (!type) {
return;
}
return type.displayName || type.name || type.key || undefined;
}
};
</script>