Skip to content

Commit

Permalink
Abstract getScriptSource into lib
Browse files Browse the repository at this point in the history
  • Loading branch information
dfrankland committed Aug 10, 2019
1 parent 278302f commit 427d8e0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 26 deletions.
19 changes: 1 addition & 18 deletions src/amphtml/components/Script.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { ReactElement } from 'react';
import PropTypes from 'prop-types';
import getScriptSource from '../../lib/getScriptSource';

export interface ScriptProps {
src?: string;
Expand All @@ -11,24 +12,6 @@ export interface ScriptProps {
type?: string;
}

interface ScriptSource {
src?: string;
extension?: string;
version?: string;
}

export const getScriptSource = ({
src = '',
extension = '',
version = 'latest',
}: ScriptSource): string => {
if (src) {
return src;
}

return `https://cdn.ampproject.org/v0/${extension}-${version}.js`;
};

const Script: React.FunctionComponent<ScriptProps> = ({
src,
extension,
Expand Down
17 changes: 17 additions & 0 deletions src/lib/getScriptSource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
interface ScriptSource {
src?: string;
extension?: string;
version?: string;
}

export default ({
src = '',
extension = '',
version = 'latest',
}: ScriptSource): string => {
if (src) {
return src;
}

return `https://cdn.ampproject.org/v0/${extension}-${version}.js`;
};
14 changes: 6 additions & 8 deletions src/setup/AmpScripts.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { ReactElement } from 'react';
import { Script, ScriptProps } from '../amphtml/amphtml';
import { getScriptSource } from '../amphtml/components/Script';
import getScriptSource from '../lib/getScriptSource';
import { AMP, AMP_SRCS, Formats } from '../constants';

export interface ScriptSource {
Expand Down Expand Up @@ -34,13 +34,11 @@ export default class AmpScripts {
}

public getScripts(): ScriptSource[] {
return Array.from(this.scripts.values()).map(
({ specName, version, src }): ScriptSource => {
return {
src: getScriptSource({ extension: specName, version, src }),
extension: specName,
};
},
return [...this.scripts.values()].map(
({ specName, version, src }): ScriptSource => ({
src: getScriptSource({ extension: specName, version, src }),
extension: specName,
}),
);
}

Expand Down

0 comments on commit 427d8e0

Please sign in to comment.