Skip to content

Instantly share code, notes, and snippets.

View moitorrijos's full-sized avatar
🤓
Working

Juan Moises Torrijos moitorrijos

🤓
Working
View GitHub Profile
@moitorrijos
moitorrijos / rename_object_keys_in_array.ts
Created April 22, 2024 15:06
A function that renames all keys in an object
const persons = [
{
name: 'Juan Perez',
nationality: 'Panamanian',
country_of_birth: 'Panama',
id_passport: '1',
date_of_birth: '2000-04-10',
},
{
name: 'Maria Perez',
@moitorrijos
moitorrijos / toTitleCase.ts
Created April 22, 2024 14:28
A function to convert any string to title case including non capitalizing words in english and spanish
function toTitleCase(str: string | undefined | null) {
if (!str) return "";
const nonCapitalizedWords = ["a", "an", "the", "and", "but", "or", "nor", "for", "yet", "so", "as", "at", "by", "for", "in", "of", "on", "per", "to", "with"];
const nonCapitalizedWordsSpanish = ["un", "una", "unos", "unas", "el", "la", "los", "las", "y", "pero", "o", "ni", "por", "aún", "así", "como", "en", "por", "para", "de", "del", "al", "con"];
return str.replace(/\w\S*/g, (txt: string) => {
if ([
...nonCapitalizedWords,
...nonCapitalizedWordsSpanish
].includes(txt.toLowerCase())) {
@moitorrijos
moitorrijos / rename_object_keys_in_array.ts
Created February 6, 2024 14:16
This script will rename all the keys of the objects inside of an array.
const persons = [
{
name: 'Juan Perez',
nationality: 'Panamanian',
country_of_birth: 'Panama',
id_passport: '1',
date_of_birth: '2000-04-10',
},
{
name: 'Maria Perez',
@moitorrijos
moitorrijos / getgreeting
Created May 26, 2013 19:09
a javascript function to display a Greeting on a website depending on the hour of the day on the client's computer.
//getGreeting function
function getGreeting (hour) {
hour = hour || new Date().getHours();
if (typeof hour === "number"){
if (hour <= 12) {
return "Good Morning";
} else if (hour <= 17) {
return "Good Afternoon";
} else {
@moitorrijos
moitorrijos / quad-equation-solver.js
Created February 18, 2013 20:10
A quadratic equation solver in JS
var quadEquationSolver = function (a, b, c) {
var rootPart = Math.sqrt( (b * b) - (4 * a * c) );
var denom = 2 * a;
var firstRoot = (-b + rootPart)/denom;
var secondRoot = (-b - rootPart)/denom;
console.log("The first root is " + firstRoot);
console.log("The second root is " + secondRoot);
};