@podium/utils
Advanced tools
Comparing version 4.1.0-next.2 to 4.1.0-next.3
@@ -17,2 +17,3 @@ import { IncomingMessage, ServerResponse } from 'http'; | ||
rel?: Pick<HTMLLinkElement, 'rel'>; | ||
type?: Pick<HTMLLinkElement, 'type'>; | ||
} | ||
@@ -19,0 +20,0 @@ |
@@ -5,2 +5,3 @@ 'use strict'; | ||
const { uriIsRelative, pathnameBuilder } = require('./utils'); | ||
const { buildLinkElement } = require('./html-utils'); | ||
@@ -28,8 +29,2 @@ // Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link | ||
const notEmpty = value => { | ||
if (value === false) return value; | ||
if (value !== '') return true; | ||
return false; | ||
}; | ||
const PodiumAssetCss = class PodiumAssetCss { | ||
@@ -165,34 +160,3 @@ constructor({ | ||
toHTML() { | ||
const args = []; | ||
args.push(`href="${this.href}"`); | ||
if (notEmpty(this.crossorigin)) { | ||
args.push(`crossorigin="${this.crossorigin}"`); | ||
} | ||
if (notEmpty(this.disabled)) { | ||
args.push('disabled'); | ||
} | ||
if (notEmpty(this.hreflang)) { | ||
args.push(`hreflang="${this.hreflang}"`); | ||
} | ||
if (notEmpty(this.title)) { | ||
args.push(`title="${this.title}"`); | ||
} | ||
if (notEmpty(this.media)) { | ||
args.push(`media="${this.media}"`); | ||
} | ||
if (notEmpty(this.as)) { | ||
args.push(`as="${this.as}"`); | ||
} | ||
args.push(`type="${this.type}"`); | ||
args.push(`rel="${this.rel}"`); | ||
return `<link ${args.join(' ')}>`; | ||
return buildLinkElement(this); | ||
} | ||
@@ -199,0 +163,0 @@ |
@@ -5,2 +5,3 @@ 'use strict'; | ||
const { uriIsRelative, pathnameBuilder } = require('./utils'); | ||
const { buildScriptElement } = require('./html-utils'); | ||
@@ -27,8 +28,2 @@ // Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script | ||
const notEmpty = value => { | ||
if (value === false) return value; | ||
if (value !== '') return true; | ||
return false; | ||
}; | ||
const PodiumAssetJs = class PodiumAssetJs { | ||
@@ -153,35 +148,3 @@ constructor({ | ||
toHTML() { | ||
const args = []; | ||
args.push(`src="${this.src}"`); | ||
if (this.type === 'esm' || this.type === 'module') { | ||
args.push('type="module"'); | ||
} | ||
if (notEmpty(this.referrerpolicy)) { | ||
args.push(`referrerpolicy="${this.referrerpolicy}"`); | ||
} | ||
if (notEmpty(this.crossorigin)) { | ||
args.push(`crossorigin="${this.crossorigin}"`); | ||
} | ||
if (notEmpty(this.integrity)) { | ||
args.push(`integrity="${this.integrity}"`); | ||
} | ||
if (notEmpty(this.nomodule)) { | ||
args.push('nomodule'); | ||
} | ||
if (notEmpty(this.async)) { | ||
args.push('async'); | ||
} | ||
if (notEmpty(this.defer)) { | ||
args.push('defer'); | ||
} | ||
return `<script ${args.join(' ')}></script>`; | ||
return buildScriptElement(this); | ||
} | ||
@@ -188,0 +151,0 @@ |
'use strict'; | ||
const buildScriptTag = ({ value = '', type = 'default' }) => { | ||
if (type === 'esm') { | ||
return `<script type="module" defer src="${value}" ></script>`; | ||
} | ||
return `<script defer src="${value}" ></script>`; | ||
}; | ||
const utils = require('./html-utils'); | ||
const buildCSSLinkTag = ({ value = '' }) => { | ||
return `<link rel="stylesheet" type="text/css" href="${value}">`; | ||
}; | ||
const document = (incoming = {}, body = '', head = '') => { | ||
@@ -20,3 +11,3 @@ let scripts = incoming.js; | ||
if (typeof incoming.js === 'string') scripts = [{ type: 'default', value: incoming.js }]; | ||
if (typeof incoming.css === 'string') styles = [{ type: 'default', value: incoming.css }]; | ||
if (typeof incoming.css === 'string') styles = [{ type: 'text/css', value: incoming.css, rel: 'stylesheet' }]; | ||
@@ -29,4 +20,4 @@ return `<!doctype html> | ||
<meta http-equiv="X-UA-Compatible" content="IE=Edge"> | ||
${styles.map(buildCSSLinkTag).join('\n ')} | ||
${scripts.map(buildScriptTag).join('\n ')} | ||
${styles.map(utils.buildLinkElement).join('\n ')} | ||
${scripts.map(utils.buildScriptElement).join('\n ')} | ||
<title>${incoming.view.title ? incoming.view.title : ''}</title> | ||
@@ -33,0 +24,0 @@ ${head} |
@@ -8,2 +8,3 @@ 'use strict'; | ||
const utils = require('./utils'); | ||
const html = require('./html-utils'); | ||
@@ -21,2 +22,4 @@ module.exports.isString = utils.isString; | ||
module.exports.deserializeContext = utils.deserializeContext; | ||
module.exports.buildScriptElement = html.buildScriptElement; | ||
module.exports.buildLinkElement = html.buildLinkElement; | ||
module.exports.HttpIncoming = HttpIncoming; | ||
@@ -23,0 +26,0 @@ module.exports.template = document; |
{ | ||
"name": "@podium/utils", | ||
"version": "4.1.0-next.2", | ||
"version": "4.1.0-next.3", | ||
"description": "Common generic utility methods shared by @podium modules.", | ||
@@ -51,3 +51,3 @@ "license": "MIT", | ||
"eslint": "^6.0.0", | ||
"eslint-config-airbnb-base": "^13.1.0", | ||
"eslint-config-airbnb-base": "^14.0.0", | ||
"eslint-config-prettier": "^6.0.0", | ||
@@ -54,0 +54,0 @@ "eslint-plugin-import": "^2.16.0", |
@@ -239,1 +239,43 @@ # Podium Utils | ||
- `data.body` - HTML body markup to be rendered | ||
### .buildLinkElement(assetCss) | ||
Build a HTML link element out of a AssetCss object. | ||
The method takes the following arguments: | ||
- assetCss - `Object` - A CSS Asset object | ||
```js | ||
const utils = require('@podium/utils'); | ||
const css = new utils.AssetCss({ | ||
value: 'https://cdn.foo.com/style.css' | ||
}); | ||
const element = utils.buildLinkElement(css); | ||
// element is: <link href="" ..... | ||
``` | ||
returns A HTML link element as a String. | ||
### .buildScriptElement(assetJs) | ||
Build a HTML script element out of a AssetJs object. | ||
The method takes the following arguments: | ||
- assetJs - `Object` - A JS Asset object | ||
```js | ||
const utils = require('@podium/utils'); | ||
const js = new utils.AssetJs({ | ||
value: 'https://cdn.foo.com/script.js' | ||
}); | ||
const element = utils.buildLinkElement(js); | ||
// element is: <script src="" ..... | ||
``` | ||
returns A HTML script element as a String. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
34316
12
749
281