Skip to content

Instantly share code, notes, and snippets.

View sergiopichardo's full-sized avatar
📚
Building

Sergio Pichardo sergiopichardo

📚
Building
View GitHub Profile
@sergiopichardo
sergiopichardo / get_foundation_models.sh
Last active March 17, 2025 02:23
Get a list of all Amazon Bedrock foundation models grouped by provider
aws bedrock list-foundation-models --region us-east-1 --query 'modelSummaries[*].[modelId]' --output json | jq -r '
flatten |
group_by(split(".")[0]) |
map({
(.[0] | split(".")[0]): map(split(".")[1])
}) |
add'
// basic-http-auth.js
function btoa(input) {
var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
input = String(input);
var bitmap, a, b, c,
result = "", i = 0,
rest = input.length % 3;
for (; i < input.length;) {
if ((a = input.charCodeAt(i++)) > 255
@sergiopichardo
sergiopichardo / preserveContextOfArgFuncWithBind.js
Created July 30, 2021 01:56
using the bind method to permanently bind a method to an object.
function fetchData() {
let sergiosTasks = {
tasks: ['study oop javascript', 'prepare for sysops exam', 'read linux book', 'write article'],
getTasks() {
console.log('Today I have to:')
this.tasks.forEach((task, index) => {
console.log(`(${index + 1}) ${task}`);
})
}
}
@sergiopichardo
sergiopichardo / functionAsArgumentContextIsLost.js
Created July 30, 2021 01:53
A function can lose its surrounding context when a function is passed as an argument to another function
function fetchData() {
let sergiosTasks = {
tasks: [
'study oop javascript',
'prepare for sysops exam',
'read linux book',
'write article'
],
getTasks() {
console.log('Today I have to:')
@sergiopichardo
sergiopichardo / preserveContextSolution4.js
Created July 30, 2021 01:48
Solution 4: Using an arrow function
class Employee {
constructor(name, lastName, salary) {
this.name = name;
this.lastName = lastName;
this.salary = salary;
}
getSalaryInfo() {
const calculateMonthlyWages = () => {
return {
@sergiopichardo
sergiopichardo / preserveContextSolution3.js
Last active July 30, 2021 12:12
Solution 3: Permanently bind the execution context using the bind method
class Employee {
constructor(name, lastName, salary) {
this.name = name;
this.lastName = lastName;
this.salary = salary;
}
getSalaryInfo() {
const calculateMonthlyWages = function() {
return {
@sergiopichardo
sergiopichardo / preserveContextSolution2.js
Created July 30, 2021 01:39
Solution 2: Call the inner function with explicit context (using call or apply)
class Employee {
constructor(name, lastName, salary) {
this.name = name;
this.lastName = lastName;
this.salary = salary;
}
getSalaryInfo() {
function calculateMonthlyWages() {
return {
@sergiopichardo
sergiopichardo / preserverExecutionContext1.js
Last active July 30, 2021 01:34
Preserve the execution context using a variable in the outer scope
class Employee {
constructor(name, lastName, salary) {
this.name = name;
this.lastName = lastName;
this.salary = salary;
}
getSalaryInfo() {
let self = this; // use a variable in outer scope to
// preserver the value of `this`
@sergiopichardo
sergiopichardo / innerFunctionDoesNotUseContext.js
Last active July 30, 2021 12:03
A function can lose its context when an inner function does not use the surrounding context
class Employee {
constructor(name, lastName, salary) {
this.name = name;
this.lastName = lastName;
this.salary = salary;
}
displaySalaryInfo() {
function calculateMonthlyWages() {
return {
@sergiopichardo
sergiopichardo / usingBindToSetContext.js
Created July 30, 2021 01:27
Permanently setting the execution context with bind
let sergiosTasks = {
tasks: ['study oop javascript', 'prepare for sysops exam', 'read linux book', 'write article'],
getTasks() {
console.log('Today I have to:')
this.tasks.forEach((task, index) => {
console.log(`(${index + 1}) ${task}`);
})
}
}