UNPKG

@jhessin/react-hyperscript-helpers

Version:

Terse syntax for hyperscript using react avoiding the use of brackets

161 lines (129 loc) 5.36 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.h = exports.hh = undefined; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _tagNames = require('./tag-names.js'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } var isArray = function isArray(x) { return Array.isArray(x); }; var isString = function isString(x) { return typeof x === 'string' && x.length > 0; }; var startsWith = function startsWith(string, start) { return string.indexOf(start) === 0; }; var isSelector = function isSelector(x) { return isString(x) && (startsWith(x, '.') || startsWith(x, '#')); }; var isChildren = function isChildren(x) { return (/string|number|boolean|function/.test(typeof x === 'undefined' ? 'undefined' : _typeof(x)) || isArray(x) || _react2.default.isValidElement(x) ); }; var split = function split(string, separator) { return string.split(separator); }; var subString = function subString(string, start, end) { return string.substring(start, end); }; var parseSelector = function parseSelector(selector) { var classIdSplit = /([\.#]?[a-zA-Z0-9\u007F-\uFFFF_:-]+)/; var parts = split(selector, classIdSplit); return parts.reduce(function (acc, part) { if (startsWith(part, '#')) { acc.id = subString(part, 1); } else if (startsWith(part, '.')) { acc.className = (acc.className + ' ' + subString(part, 1)).trim(); } return acc; }, { className: '' }); }; var createElement = function createElement(nameOrType) { var properties = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var children = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : []; if (properties.isRendered !== undefined && !properties.isRendered) { return null; } var props = _objectWithoutProperties(properties, []); var args = [nameOrType, props]; if (!isArray(children)) { args.push(children); } else { args.push.apply(args, _toConsumableArray(children)); } return _react2.default.createElement.apply(_react2.default, args); }; var hh = exports.hh = function hh(nameOrType) { return function (first, second, third) { for (var _len = arguments.length, rest = Array(_len > 3 ? _len - 3 : 0), _key = 3; _key < _len; _key++) { rest[_key - 3] = arguments[_key]; } rest = rest || []; var parseChild = function parseChild(child) { if (child instanceof Array) { rest = [].concat(_toConsumableArray(child), _toConsumableArray(rest)); } else { rest = [child].concat(_toConsumableArray(rest)); } }; if (isSelector(first)) { var selector = parseSelector(first); // selector, ...children if (isChildren(second)) { if (isChildren(third)) { parseChild(third); } parseChild(second); return createElement(nameOrType, selector, rest); } // selector, props, ...children var _ref = second || {}, _ref$className = _ref.className, className = _ref$className === undefined ? '' : _ref$className; className = (selector.className + ' ' + className + ' ').trim(); var props = _extends({}, second, selector, { className: className }); if (isChildren(third)) { parseChild(third); return createElement(nameOrType, props, rest); } return createElement(nameOrType, props); } // children if (isChildren(first)) { if (isChildren(second)) { if (isChildren(third)) { parseChild(third); } parseChild(second); } parseChild(first); return createElement(nameOrType, {}, rest); } // props, children if (isChildren(second)) { if (isChildren(third)) { parseChild(third); } parseChild(second); return createElement(nameOrType, first, rest); } return createElement(nameOrType, first); }; }; var h = exports.h = function h(nameOrType) { for (var _len2 = arguments.length, rest = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { rest[_key2 - 1] = arguments[_key2]; } return hh(nameOrType).apply(undefined, rest); }; module.exports = _tagNames.TAG_NAMES.reduce(function (exported, type) { exported[type] = hh(type); return exported; }, { h: h, hh: hh });