Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

api-elements

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

api-elements - npm Package Compare versions

Comparing version 0.2.1 to 0.2.2

test/api-description-test.js

4

CHANGELOG.md
# API Elements (JavaScript) CHANGELOG
## 0.2.2 (2019-07-02)
Internal changes.
## 0.2.1 (2019-06-19)

@@ -4,0 +8,0 @@

4

lib/api-elements.js

@@ -50,4 +50,4 @@ const minim = require('minim');

defineValueOf(this);
defineSourceMapValue(this);
defineValueOf();
defineSourceMapValue();
}

@@ -54,0 +54,0 @@ }

@@ -1,4 +0,4 @@

module.exports = (namespace) => {
const { Element } = namespace;
const { Element } = require('minim');
module.exports = () => {
/**

@@ -5,0 +5,0 @@ * @name sourceMapValue

/* eslint-disable no-bitwise, no-underscore-dangle */
const R = require('ramda');
const {
Element,
ArrayElement,
ObjectElement,
StringElement,
BooleanElement,
NumberElement,
NullElement,
} = require('minim');
const EnumElement = require('./elements/Enum');
const setFlag = (mask, options) => (options | mask);

@@ -23,7 +37,8 @@ const clearFlag = (mask, options) => (options & ~mask);

if (undefined !== e._attributes) {
const attrs = e.attributes.get('typeAttributes');
if (undefined !== attrs && undefined !== attrs.content) {
return attrs.content.some(attr => attr.content === attribute);
const typeAttributes = e.attributes.get('typeAttributes');
if (typeAttributes) {
return typeAttributes.contains(attribute);
}
}
return false;

@@ -39,11 +54,15 @@ }

let result = options;
if (hasFixedTypeAttribute(e)) {
result = setFlag(FIXED_FLAG, result);
}
if (hasFixedTypeTypeAttribute(e)) {
result = setFlag(FIXED_TYPE_FLAG, result);
}
if (hasNullableTypeAttribute(e)) {
result = setFlag(NULLABLE_FLAG, result);
}
return result;

@@ -56,65 +75,94 @@ }

module.exports = (namespace) => {
const { Element } = namespace.elements;
const {
Array: ArrayElement,
Object: ObjectElement,
String: StringElement,
Boolean: BooleanElement,
Number: NumberElement,
Null: NullElement,
Enum: EnumElement,
} = namespace.elements;
function findFirstSample(e) {
if (undefined !== e._attributes) {
const samples = e.attributes.get('samples');
if (samples instanceof ArrayElement) {
if (undefined !== samples.content && samples.content.length > 0) {
return samples.content[0];
}
function findFirstSample(e) {
if (undefined !== e._attributes) {
const samples = e.attributes.get('samples');
if (samples instanceof ArrayElement) {
if (undefined !== samples.content && samples.content.length > 0) {
return samples.content[0];
}
}
return null;
}
const isPrimitive = e => (e instanceof StringElement) || (e instanceof NumberElement) || (e instanceof BooleanElement);
return null;
}
function trivialValue(e) {
if (e instanceof BooleanElement) {
return new BooleanElement(false);
const isPrimitive = e => (e instanceof StringElement) || (e instanceof NumberElement) || (e instanceof BooleanElement);
const isEnumElement = e => e instanceof EnumElement;
const isPlural = e => e instanceof ArrayElement;
function trivialValue(e) {
if (e instanceof BooleanElement) {
return new BooleanElement(false);
}
if (e instanceof NumberElement) {
return new NumberElement(0);
}
if (e instanceof StringElement) {
return new StringElement('');
}
if (e instanceof NullElement) {
return new NullElement();
}
return undefined;
}
function mapValue(e, options, f, elements) {
const opts = updateTypeAttributes(e, options);
if (e.content && (!isPlural(e) || !e.isEmpty)) {
const result = f(e, opts, elements, 'content');
if (undefined !== result) {
return result;
}
if (e instanceof NumberElement) {
return new NumberElement(0);
}
const sample = findFirstSample(e);
if (sample) {
const result = f(sample, opts, elements, 'sample');
if (undefined !== result) {
return result;
}
if (e instanceof StringElement) {
return new StringElement('');
}
const dflt = findDefault(e);
if (dflt) {
const result = f(dflt, opts, elements, 'default');
if (undefined !== result) {
return result;
}
if (e instanceof NullElement) {
return new NullElement();
}
if (isFlag(NULLABLE_FLAG, opts)) {
const result = f(new NullElement(), opts, elements, 'nullable');
if (undefined !== result) {
return result;
}
return undefined;
}
const isEnumElement = e => e instanceof EnumElement;
const isPlural = e => e instanceof ArrayElement;
if (elements) {
if (e.element === 'ref') {
const result = elements[e.content];
const inheritedElements = R.filter(el => !el.id.equals(e.content), elements);
function mapValue(e, options, f, elements) {
const opts = updateTypeAttributes(e, options);
if (e.content && (!isPlural(e) || e.content.length > 0)) {
const result = f(e, opts, elements, 'content');
if (undefined !== result) {
return result;
if (e.path && e.path.toValue() === 'content') {
return mapValue(result.content, opts, f, inheritedElements);
}
return mapValue(result, opts, f, inheritedElements);
}
const sample = findFirstSample(e);
if (sample) {
const result = f(sample, opts, elements, 'sample');
if (undefined !== result) {
return result;
}
const result = elements[e.element];
if (result) {
const inheritedElements = R.filter(el => !el.id.equals(e.element), elements);
return mapValue(result, opts, f, inheritedElements);
}
const dflt = findDefault(e);
if (dflt) {
const result = f(dflt, opts, elements, 'default');
}
if (isEnumElement(e)) {
const enums = e.enumerations;
if (enums && enums.content && enums.content[0]) {
const result = f(enums.content[0], opts, elements, 'generated');
if (undefined !== result) {

@@ -124,110 +172,92 @@ return result;

}
if (isFlag(NULLABLE_FLAG, opts)) {
const result = f(new NullElement(), opts, elements, 'nullable');
if (undefined !== result) {
return result;
}
}
const trivial = trivialValue(e);
if (trivial) {
const result = f(trivial, opts, elements, 'generated');
if (undefined !== result) {
return result;
}
}
if (elements) {
if (e.element === 'ref') {
const result = elements.find(el => el.id.equals(e.content));
const inheritedElements = elements.filter(el => !el.id.equals(e.content));
if (isPlural(e) && e.isEmpty) {
return f(e, opts, elements, 'generated');
}
if (e.path && e.path.toValue() === 'content') {
return mapValue(result.content, opts, f, inheritedElements);
}
return undefined;
}
return mapValue(result, opts, f, inheritedElements);
}
function reduceValue(e, options, elements) {
const opts = updateTypeAttributes(e, options);
const result = elements.find(el => el.id.equals(e.element));
if (result) {
const inheritedElements = elements.filter(el => !el.id.equals(e.element));
return mapValue(result, opts, f, inheritedElements);
}
}
if (e.content === undefined) {
return mapValue(e, opts, e => e.content, elements);
}
if (isEnumElement(e)) {
const enums = e.enumerations;
if (enums && enums.content && enums.content[0]) {
const result = f(enums.content[0], opts, elements, 'generated');
if (undefined !== result) {
return result;
}
}
}
const trivial = trivialValue(e);
if (trivial) {
const result = f(trivial, opts, elements, 'generated');
if (undefined !== result) {
return result;
}
}
if (isPlural(e) && e.content.length === 0) {
return f(e, opts, elements, 'generated');
}
if (isPrimitive(e)) {
return e.content;
}
return undefined;
if (e instanceof NullElement) {
return null;
}
function reduceValue(e, options, elements) {
const opts = updateTypeAttributes(e, options);
if (undefined === e.content) {
return mapValue(e, opts, e => e.content, elements);
}
if (isPrimitive(e)) {
return e.content;
}
if (e instanceof NullElement) {
return null;
}
if (isEnumElement(e)) {
return mapValue(e.content, inheritFlags(opts), reduceValue, elements);
}
if (e instanceof ObjectElement) {
let result = {};
e.content.some((item) => {
const skippable = hasOptionalTypeAttribute(item)
|| (!isFlag(FIXED_FLAG, opts)
&& !isFlag(FIXED_TYPE_FLAG, opts)
&& !hasTypeAttribute(item, 'required'));
if (isEnumElement(e)) {
return mapValue(e.content, inheritFlags(opts), reduceValue, elements);
}
const k = mapValue(item.key, inheritFlags(opts), reduceValue, elements);
if (e instanceof ObjectElement) {
let result = {};
if (undefined === k) {
if (skippable) {
return false;
}
result = undefined;
return true;
const isFixed = isFlag(FIXED_FLAG, opts);
const isFixedType = isFlag(FIXED_TYPE_FLAG, opts);
e.content.some((item) => {
const skippable = hasOptionalTypeAttribute(item) || (!isFixed && !isFixedType && !hasTypeAttribute(item, 'required'));
const key = mapValue(item.key, inheritFlags(opts), reduceValue, elements);
if (key === undefined) {
if (skippable) {
return false;
}
const v = mapValue(item.value, inheritFlags(opts), reduceValue, elements);
if (undefined === v) {
if (skippable) {
return false;
}
result = undefined;
return true;
result = undefined;
return true;
}
const value = mapValue(item.value, inheritFlags(opts), reduceValue, elements);
if (value === undefined) {
if (skippable) {
return false;
}
result[k] = v;
return false;
});
return result;
}
if (e instanceof ArrayElement) {
const result = e.map(item => mapValue(item, inheritFlags(opts), reduceValue, elements));
if (!isFlag(FIXED_FLAG, opts) && !isFlag(FIXED_TYPE_FLAG, opts)) {
return result.filter(item => item !== undefined);
result = undefined;
return true;
}
if (result.includes(undefined)) {
return undefined;
}
return result;
result[key] = value;
return false;
});
return result;
}
if (e instanceof ArrayElement) {
const result = e.map(item => mapValue(item, inheritFlags(opts), reduceValue, elements));
if (!isFlag(FIXED_FLAG, opts) && !isFlag(FIXED_TYPE_FLAG, opts)) {
return result.filter(item => item !== undefined);
}
return undefined;
if (result.includes(undefined)) {
return undefined;
}
return result;
}
return undefined;
}
module.exports = () => {
if (!Object.getOwnPropertyNames(Element.prototype).includes('valueOf')) {

@@ -234,0 +264,0 @@ Object.defineProperty(Element.prototype, 'valueOf', {

{
"name": "api-elements",
"version": "0.2.1",
"version": "0.2.2",
"description": "API Elements JavaScript",

@@ -8,3 +8,2 @@ "author": "Apiary.io <support@apiary.io>",

"main": "lib/api-elements.js",
"files": ["lib/*.js", "lib/elements/*.js"],
"homepage": "https://github.com/apiaryio/api-elements.js/tree/master/packages/api-elements",

@@ -22,12 +21,14 @@ "repository": {

"dependencies": {
"minim": "^0.23.4"
"minim": "^0.23.5",
"ramda": "^0.26.1"
},
"devDependencies": {
"chai": "^4.1.2",
"eslint": "^5.9.0",
"mocha": "^5.0.2"
"chai": "^4.2.0",
"eslint": "^5.16.0",
"mocha": "^5.2.0"
},
"engines": {
"node": ">=8"
}
},
"gitHead": "c0fd7dd00ddf8320287461ff0ba4932d12f2e82e"
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc