import {coreMethods,nodeRows,validatorGroups} from './docs/reference.js';
const playgroundKey='strong-type:playground-source';
const escapeHTML=function(value){
return String(value).replace(/[&<>"']/g,character=>({
'&':'&',
'<':'<',
'>':'>',
'"':'"',
"'":'''
})[character]);
};
const statusClass=function(runtime){
if(runtime === 'Shared'){
return 'shared';
}
if(runtime === 'Node'){
return 'node';
}
if(runtime.includes('Web')){
return 'web';
}
return 'guarded';
};
const rowHTML=function(current,includeTry=true){
const tryCell=includeTry ? `
| ` : '';
return `
is.${escapeHTML(current.method)}(value) |
${escapeHTML(current.accepts)} |
${escapeHTML(current.edge)} |
${escapeHTML(current.runtime)} |
${escapeHTML(current.example)} |
${tryCell}
`;
};
const renderValidators=function(query=''){
const target=document.querySelector('#validator-tables');
const status=document.querySelector('#filter-status');
if(!target || !status){
return;
}
const normalized=query.trim().toLowerCase();
let visible=0;
const groups=validatorGroups.map(group=>{
const rows=group.rows.filter(current=>!normalized || Object.values(current).join(' ').toLowerCase().includes(normalized));
visible+=rows.length;
if(!rows.length){
return '';
}
return `
${escapeHTML(group.title)}
${escapeHTML(group.description)}
${rows.length} validator${rows.length === 1 ? '' : 's'}
| Method | What passes | Important edge | Runtime | Example | Run |
${rows.map(current=>rowHTML(current)).join('')}
`;
}).join('');
target.innerHTML=groups || 'No validators match that filter.
';
status.textContent=`${visible} validator${visible === 1 ? '' : 's'} shown`;
};
const renderCore=function(){
const target=document.querySelector('#core-table');
if(!target){
return;
}
const rows=coreMethods.map(current=>`is.${escapeHTML(current.method)} | ${escapeHTML(current.result)} | ${escapeHTML(current.purpose)} |
`).join('');
target.innerHTML=`Core and extension methods| Method | Result | Purpose |
${rows}
`;
};
const renderNode=function(){
const target=document.querySelector('#node-table');
if(!target){
return;
}
const rows=nodeRows.map(current=>rowHTML(current,false)).join('');
target.innerHTML=`Node-only validators| Method | What passes | Important edge | Runtime | Example |
${rows}
`;
};
const copyText=async function(value,button){
try{
await navigator.clipboard.writeText(value);
const previous=button.textContent;
button.textContent='Copied';
setTimeout(()=>{button.textContent=previous;},1200);
}catch(err){
button.textContent='Copy failed';
}
};
const setupCopy=function(){
document.addEventListener('click',event=>{
const button=event.target.closest('[data-copy],[data-copy-target]');
if(!button){
return;
}
const direct=button.dataset.copy;
const target=button.dataset.copyTarget && document.getElementById(button.dataset.copyTarget);
copyText(direct || (target && target.textContent) || '',button);
});
};
const storePlaygroundSource=function(source){
try{
sessionStorage.setItem(playgroundKey,source);
}catch(err){
return false;
}
return true;
};
const postPlaygroundSource=function(frame,source){
frame.contentWindow.postMessage({type:'strong-type:load',source},location.origin);
};
const openPlayground=function(source){
const frame=document.querySelector('#playground-frame');
if(frame){
if(frame.dataset.ready === 'true'){
postPlaygroundSource(frame,source);
}else{
frame.addEventListener('load',()=>postPlaygroundSource(frame,source),{once:true});
}
frame.focus();
return;
}
storePlaygroundSource(source);
location.href='./playground.html';
};
const setupExamples=function(){
document.addEventListener('click',event=>{
const exampleButton=event.target.closest('[data-example]');
if(exampleButton){
const source=`import Is from 'strong-type';\n\nconst is=new Is;\nconsole.log(${exampleButton.dataset.example});`;
openPlayground(source);
return;
}
const loadButton=event.target.closest('[data-load-target]');
if(!loadButton){
return;
}
const target=document.getElementById(loadButton.dataset.loadTarget);
if(target){
openPlayground(target.textContent);
}
});
};
const setupPlayground=function(){
const frame=document.querySelector('#playground-frame');
if(!frame){
return;
}
const ready=function(){
frame.dataset.ready='true';
};
frame.addEventListener('load',ready,{once:true});
let source;
try{
source=sessionStorage.getItem(playgroundKey);
sessionStorage.removeItem(playgroundKey);
}catch(err){
source=false;
}
if(!source){
return;
}
frame.addEventListener('load',()=>postPlaygroundSource(frame,source),{once:true});
try{
if(frame.contentDocument && frame.contentDocument.readyState === 'complete'){
ready();
postPlaygroundSource(frame,source);
}
}catch(err){
return;
}
};
const setupNavigation=function(){
const toggle=document.querySelector('.nav-toggle');
const navigation=document.querySelector('#top-nav');
if(!toggle || !navigation){
return;
}
toggle.addEventListener('click',()=>{
const open=navigation.classList.toggle('open');
toggle.setAttribute('aria-expanded',String(open));
});
navigation.addEventListener('click',()=>{
navigation.classList.remove('open');
toggle.setAttribute('aria-expanded','false');
});
};
renderValidators();
renderCore();
renderNode();
setupCopy();
setupExamples();
setupPlayground();
setupNavigation();
const filter=document.querySelector('#type-filter');
if(filter){
filter.addEventListener('input',event=>renderValidators(event.target.value));
}