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

leaf-converter

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

leaf-converter - npm Package Compare versions

Comparing version 1.8.2 to 1.9.0

11

CHANGELOG.md

@@ -5,2 +5,13 @@ # Change Log

<a name="1.9.0"></a>
# [1.9.0](https://github.com/forsigner/leaf-converter/compare/v1.8.2...v1.9.0) (2018-02-01)
### Features
* can getProps ([aadd946](https://github.com/forsigner/leaf-converter/commit/aadd946))
* support block comment ([a891a5e](https://github.com/forsigner/leaf-converter/commit/a891a5e))
<a name="1.8.2"></a>

@@ -7,0 +18,0 @@ ## [1.8.2](https://github.com/forsigner/leaf-converter/compare/v1.8.1...v1.8.2) (2018-01-25)

177

lib/object-to-schema/json.js

@@ -8,14 +8,15 @@ 'use strict';

var keywords = ['maxLength', 'minLength', 'pattern', 'enum', 'minimum', 'maximum', 'required', 'maxProperties', 'minProperties', 'additionalProperties', 'minItems', 'maxItems', 'uniqueItems'];
var _ = require('lodash');
var separator = '(@#$%)';
module.exports = objectToSchema;
var rules = [[_.isNull, function (value) {
return handleValue('null', value);
return value;
}], [_.isNumber, function (value) {
return handleValue('number', value);
return value;
}], [_.isBoolean, function (value) {
return handleValue('boolean', value);
return value;
}], [_.isString, function (value) {
return handleValue('string', value);
return value;
}], [_.isRegExp, function (pattern) {

@@ -50,3 +51,33 @@ return { type: 'string', pattern: pattern };

_.keys(object).forEach(function (item) {
// TODO
if (item.indexOf('//') > -1) return;
var _loop = function _loop(isMatch, makeSchema) {
if (!isMatch(object[item])) {
return 'continue';
}
if (_.isPlainObject(object[item])) {
obj[item] = makeSchema(object[item]);
return 'continue';
}
var type = typeOf(object[item]);
var commentValue = object['// ' + item];
var value = {
default: object[item],
type: type
};
// handle comment
if (commentValue) {
var props = getProps(commentValue);
Object.keys(props).forEach(function (key) {
value[key] = props[key];
});
}
obj[item] = makeSchema(value);
return 'break';
};
var _iteratorNormalCompletion = true;

@@ -57,3 +88,3 @@ var _didIteratorError = false;

try {
for (var _iterator = rules[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
_loop2: for (var _iterator = rules[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var _ref = _step.value;

@@ -66,20 +97,10 @@

if (!isMatch(object[item])) {
continue;
}
var _ret = _loop(isMatch, makeSchema);
if (_.isPlainObject(object[item])) {
obj[item] = makeSchema(object[item]);
continue;
}
switch (_ret) {
case 'continue':
continue;
if (object['// ' + item]) {
var type = typeOf(object[item]);
var desc = getDesc(object['// ' + item]);
var value = '' + object[item] + separator + type + separator + desc;
obj[item] = makeSchema(value);
} else {
obj[item] = makeSchema(object[item]);
}
break;
case 'break':
break _loop2;}
}

@@ -104,9 +125,69 @@ } catch (err) {

function getDesc(desc) {
if (_.isString(desc)) return desc;
if (_.isArray(desc) && desc.length === 2) {
return desc[1][0].replace(/^\/\//, '').trim();
function getProps(commentValue) {
var props = {
description: getDesc(commentValue)
};
var blockComment = getBlockComment(commentValue);
if (!blockComment) return props;
// block comment
var commentLines = blockComment.split('\n');
commentLines.forEach(function (commentLine) {
var matchKeywod = getMatchKeyword(commentLine);
if (!matchKeywod) return;
var value = getValueByKeyword(commentLine, matchKeywod);
props[matchKeywod] = handleValueByKeywordType(value, matchKeywod);
});
return props;
}
function getValueByKeyword(commentLine, matchKeywod) {
var arr = commentLine.split(matchKeywod);
return (_.last(arr) || '').trim();
}
function handleValueByKeywordType(value, matchKeywod) {
if (matchKeywod === 'enum') {
/* eslint-disable no-new-func */
return new Function('return (' + value + ')')();
}
return isNumKeyword(matchKeywod) ? parseInt(value, 10) || 0 : value;
}
function getMatchKeyword(commentLine) {
return keywords.find(function (word) {
return commentLine.indexOf(word) > -1;
});
}
function isBlockComment() {
var str = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
return str.indexOf('/**') > -1;
}
function getBlockComment(commentValue) {
if (!commentValue[0] || !_.isArray(commentValue[0])) return null;
return commentValue[0].find(function (item) {
return isBlockComment(item);
});
}
function getDesc(commentValue) {
var desc = '';
// 兼容旧代码
if (_.isString(commentValue)) {
desc = commentValue;
}
// get description from comment
if (_.isArray(commentValue) && commentValue.length === 2) {
desc = commentValue[1][0].replace(/^\/\//, '').trim();
}
return desc;
}
function objectToSchema(data) {

@@ -148,37 +229,2 @@ var _iteratorNormalCompletion2 = true;

// handle this type data: Hackers and Painters|string|book name
function handleValue(type, value) {
if (!_.isString(value)) {
return { type: type, default: value };
}
var arr = value.split(separator);
if (arr.length < 2) {
return { type: type, default: value };
}
var _arr = _slicedToArray(arr, 3),
defaultValue = _arr[0],
valueType = _arr[1],
description = _arr[2];
var maps = {
null: function _null() {
return null;
},
number: Number,
boolean: function boolean(data) {
return data === 'true';
},
string: function string(data) {
return data;
}
};
return {
type: valueType,
default: maps[valueType](defaultValue),
description: description
};
}
function typeOf(data) {

@@ -197,6 +243,6 @@ var rules = [[_.isNull, 'null'], [_.isNumber, 'number'], [_.isBoolean, 'boolean'], [_.isString, 'string'], [_.isRegExp, 'string']];

var isMatch = _ref6[0];
var value = _ref6[1];
var _value = _ref6[1];
if (isMatch(data)) {
return value;
return _value;
}

@@ -218,2 +264,7 @@ }

}
}
function isNumKeyword(keyword) {
var numKeywords = ['maxLength', 'minLength', 'minimum', 'maximum', 'maxProperties', 'minProperties', 'minItems', 'maxItems'];
return numKeywords.indexOf(keyword) > -1;
}

@@ -21,7 +21,11 @@ 'use strict';

if (comment) {
var _comment = [null, null];
if (properties[key].description) {
_comment[1] = ['// ' + properties[key].description];
}
// TODO 兼容 json-comment
properties['// ' + key] = [null, ['// ' + properties[key].description]];
if (properties[key].maxLength) {
_comment[0] = ['/**\n * @maxLength ' + properties[key].maxLength + '\n */'];
}
properties['// ' + key] = _comment;
}

@@ -28,0 +32,0 @@

{
"name": "leaf-converter",
"description": "",
"version": "1.8.2",
"version": "1.9.0",
"main": "lib/index.js",

@@ -6,0 +6,0 @@ "author": {

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