Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 0 additions & 72 deletions .circleci/config.yml

This file was deleted.

4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
node_modules
index.js
index.d.ts
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
index.ts
29 changes: 0 additions & 29 deletions index.js

This file was deleted.

45 changes: 45 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
interface URL {
type: 'script' | 'style';
url: string;
options?: {
async: boolean;
defer: boolean;
}
}

function makeScript(url: string, async: boolean, defer: boolean) {
const tag = document.createElement('script')
tag.src = url
tag.async = async
tag.defer = defer
return tag
}

function makeLink(url: string) {
const tag = document.createElement('link')
tag.rel = 'stylesheet'
tag.href = url
return tag
}

export default function (urls: URL[], test: () => boolean, callback: () => void) {
let remaining = urls.length

function maybeCallback () {
remaining = --remaining
if (remaining < 1) {
callback()
}
}

if (!test()) {
urls.forEach(({ type, url, options = { async: true, defer: true }}) => {
const isScript = type === 'script'
const tag = isScript ? makeScript(url, options.async, options.defer) : makeLink(url)
tag.onload = maybeCallback
document.body.appendChild(tag)
})
} else {
callback()
}
}
Loading