cliches demo

node v14.20.1
version: 1.0.5
endpointsharetweet
A writting utility that uses a regular expression glossary to dynamically identify and list commonly used clichés
Javascript example (using POST)
/* const url = "https://cliches-r6gafch48odl.runkit.sh"; const text = 'as "ugly as sin itself" and yet he was totally under her thumb'; const body = JSON.stringify(text); fetch(url, { method: "POST", body }) .then(d => d.json()) .then(console.log); */
CURL example (using GET)
/* curl https://cliches-r6gafch48odl.runkit.sh?text=as%20ugly%20as%20sin%20itself%20oh%20... */
const cliches = require('cliches'); function querystring(url = null) { const [_, text] = unescape(url).match(/text=(.*)/) || [null, null]; return text; } function allowOrigin(origin = "*") { return { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': origin, 'Access-Control-Allow-Methods': 'POST, GET', }; } function getCliches(string = null, content = []) { const body = content.length ? JSON.parse(content.join('')) : null; const text = body && body.text ? body.text : string; const results = cliches.test(text); return { results }; } exports.endpoint = function (request, response) { const timeStamp = new Date().valueOf(); const { url, method = 'GET' } = request; const string = querystring(url); let content = []; request.on('data', (c) => content.push(c)); request.on('end', () => { const options = allowOrigin(); const results = getCliches(string, content); response.writeHead(200, options); response.end(JSON.stringify({ results, method, timeStamp })); }); };
Loading…

no comments

    sign in to comment