This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function roundToPow(n, pow = 10) { | |
const p = Math.pow(pow, Math.round(n).toString().length - 1); | |
return Math.round(n / p) * p; | |
} | |
console.log(roundToPow(997)); // 1000 | |
console.log(roundToPow(12457)); // 10000 | |
console.log(roundToPow(13,3)); // 12 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const objectToString = (o) => { | |
return Object.keys(o) | |
.map((k) => `${encodeURIComponent(k)}=${encodeURIComponent(o[k])}`) | |
.join('&'); | |
}; | |
const obj = {foo: "bar", abc: "xyz", name: "John Doe"}; | |
const str = objectToString(obj); | |
console.log(str); // foo=bar&abc=xyz&name=John%20Doe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$nbChars = 50; | |
$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus in ullamcorper est. Quisque semper commodo maximus. Pellentesque pharetra libero lorem, at scelerisque eros sollicitudin ac. Phasellus ullamcorper sem nisi, in tristique augue tincidunt eu. Vivamus congue at felis at porttitor. Curabitur eget urna ac arcu pharetra blandit. Etiam non facilisis ante. Proin iaculis consequat leo sit amet faucibus. Nulla sed venenatis nisl, eget placerat dolor. Donec aliquam egestas dui vitae faucibus. Quisque eget suscipit mauris. Vivamus eget sem erat."; | |
echo abstract($text, $nbChars); | |
function abstract(string $text="", int $nbChars=50) { | |
$abstract = explode("\n", wordwrap($text, $nbChars)); | |
return $abstract[0] . " ..."; | |
} | |
?> |