Skip to content

Instantly share code, notes, and snippets.

View jagomf's full-sized avatar

Jago jagomf

View GitHub Profile
@jagomf
jagomf / replace-in-array.js
Created June 4, 2018 14:34
Replace element in array using Object.assign
const arr = ['a', 'b', 'c', 'd', 'e'];
const postToReplace = 2; // position 2 points to letter 'c' in the array
const newArr = Object.assign([...arr], {[postToReplace]: 'J'});
console.log(newArr);
// ['a', 'b', 'J', 'd', 'e']
@jagomf
jagomf / linux.sh
Last active July 29, 2020 19:10
Linux console commands to remember
## Look for an ELAN device ID
$ xinput list
## Use ELAN device ID (in this case 14)
$ xinput map-to-output 14 eDP-1
## Kill React Native script that prevents deleting node_modules
@jagomf
jagomf / app.routing.ts
Created April 6, 2018 07:59
Nested routes in Angular 2+
...
{ path: 'wall', component: WallHandlerComponent, canActivate: [LoggedGuard],
children: [
{ path: '', redirectTo: 'recent', pathMatch: 'full' },
{ path: 'recent', component: RecentEventsComponent },
{ path: 'ranking', component: RankingUsersComponent }
] },
...
@jagomf
jagomf / install.sh
Created February 13, 2018 15:55
Install and build project from non-npm git repo
npm i github:jagomf/project-name#semver:^3.0.0
@jagomf
jagomf / git_practices
Last active February 8, 2018 10:50
Git submodules best practices
// Clone repository with submodules automatically:
git clone --recursive git@github.com:name/repo.git
// Initialize submodules after regular cloning:
git submodule update --init
// Make submodules to track their respective remote branches (instead of being in detached HEAD state):
@jagomf
jagomf / open-and-resize-on-click.js
Last active September 29, 2018 17:28
Retrieve files and convert to base64 upon clicking on something; if any of the files are images, they can be limited to a max size (in pixels)
/**
* Prompts to choose a file from filesystem, converts to base64 (in background), and returns base64 when conversion ends.
* @param {number} maxFileSize Maximum size of file.
* @param {boolean} multiple Indicates whether to accept one or more files.
* @param {string} contentType MIME type of media to require from filesystem (can be 'image/*', etc).
* @param {number} maxSize Maximum size (in pixels) that images' largest dimention (width or length) should have, or 0 to ignore.
* @param {string} finalMimeType Mime type of the resulting resized image.
* @param {string} source (For mobile devices only) If set to 'camera', tells device to capture image from camera.
* @returns {Promise} A Promise that resolves when files are read; rejects with error if files are heavier than allowed.
*/
@jagomf
jagomf / resize-image.js
Last active September 29, 2018 17:47
Resizes a base64 image to a specified maximum largest dimension (whether width or height)
/**
* Resizes a picture to a maximum length/width (based on largest dimension)
* @param {string} original Base64 representation of image to be resized.
* @param {number} maxSize Amount of pixels that largest dimention (whether width or length) should have.
* @param {string} finalMimeType Mime type of the resulting resized image.
* @returns {Promise} A Promise that resolves with resized picture.
*/
resizeImage(original, maxSize, finalMimeType = 'image/jpeg') {
return new Promise(resolve => {
const image = new Image();
@jagomf
jagomf / limit-lines.css
Created November 3, 2017 12:21
Limita un texto en CSS al número de líneas especificado y luego le pone elipsis
limitLines {
overflow: hidden,
display: -webkit-box,
-webkit-line-clamp: 2,
-webkit-box-orient: vertical
}
@jagomf
jagomf / open-file-on-click.js
Last active September 29, 2018 17:34
Retrieve a file and convert to base64 upon clicking on something
/**
* Prompts to choose a file from filesystem, converts to base64 (in background), and returns base64 when conversion ends.
* @param {number} maxFileSize Maximum size of file.
* @param {boolean} multiple Indicates whether to accept one or more files.
* @param {string} contentType MIME type of media to require from filesystem (can be 'image/*', etc).
* @param {string} source (For mobile devices only) If set to 'camera', tells device to capture image from camera.
* @returns {Promise} A Promise that resolves when files are read; rejects with error text if files are heavier than allowed.
*/
onClickSendMedia(maxFileSize, multiple = false, contentType = '*/*', source) {
return new Promise((resolve, reject) => {
@jagomf
jagomf / listJoiner.js
Last active August 29, 2015 14:20
Turns an array of items into a String of items separated by middleJoiner and endJoiner
/*
var fruits = ['apple', 'orange', 'banana', 'pear'],
middleJoiner = ', ',
endJoiner = ' and ',
result = listJoiner(fruits, middleJoiner, endJoiner);
*/
// result will be this string: 'apple, orange, banana and pear'
//Iterative way
function listJoinerIter(elems, middleJoiner, endJoiner) {