Skip to content

Instantly share code, notes, and snippets.

View cms's full-sized avatar

Christian C. Salvadó cms

View GitHub Profile
@cms
cms / getStyle.js
Created April 17, 2010 00:48
Get computed styles
function getStyle(el, styleProp) {
var value, defaultView = el.ownerDocument.defaultView;
// W3C standard way:
if (defaultView && defaultView.getComputedStyle) {
// sanitize property name to css notation (hypen separated words eg. font-Size)
styleProp = styleProp.replace(/([A-Z])/g, "-$1").toLowerCase();
return defaultView.getComputedStyle(el, null).getPropertyValue(styleProp);
} else if (el.currentStyle) { // IE
// sanitize property name to camelCase
styleProp = styleProp.replace(/\-(\w)/g, function(str, letter) {
@cms
cms / es3-es5-incompatibilities.js
Created October 27, 2010 18:55
List of ES3 Incompatibilities introduced by ES5
/*
* List of ES3 Incompatibilities introduced by ES5.
*
*/
/*
* From Annex E:
*/
@cms
cms / Optimizar.js
Last active January 10, 2020 14:55
import _ from "lodash"
import React from "react"
import moment from "moment"
import { connect } from "react-redux"
import API from "@Services/Api/profile"
import { ApiManager } from "../../api/apiManager"
import ParsedText from "react-native-parsed-text"
import { NavigationEvents } from "react-navigation"
import { DateToWordsFromNow } from "../../helpers/helper"
import { ACTIVITY_NOTIFICATION } from "../../api/constants"
import _ from "lodash";
import React from 'react';
import moment from 'moment';
import { connect } from 'react-redux';
import API from '@Services/Api/profile';
import { ApiManager } from "../../api/apiManager";
import ParsedText from 'react-native-parsed-text';
import { NavigationEvents } from "react-navigation";
import { DateToWordsFromNow } from '../../helpers/helper';
import { ACTIVITY_NOTIFICATION } from "../../api/constants";
@cms
cms / getCurrentPosition.js
Created August 13, 2019 18:08
navigator.geolocation.getCurrentPosition promise
function getCurrentPosition(options) {
return new Promise(function(resolve, reject) {
navigator.geolocation.getCurrentPosition(resolve, reject, options);
});
}
(async () => {
const position = await getCurrentPosition().catch(err => console.log(err));
console.log(position);
})();
@cms
cms / debounce.js
Created July 24, 2019 19:14
Small debounce function
/**
* Small debounce function.
*
* by Christian C. Salvadó <c@cms.gt>
* MIT Style license, 2019
*/
function debounce(fn, ms = 0) {
let timer = 0
@cms
cms / fetch-timeout.js
Last active June 4, 2019 16:45
fetch with cancellation at timeout using AbortController
export default function fetchWithTimeout(url, options, timeout = 3000) {
const controller = new AbortController()
const { signal } = controller
setTimeout(() => controller.abort(), timeout)
return fetch(url, { ...options, signal })
}
@cms
cms / capitalizeSentence.js
Last active May 10, 2019 22:14
capitalizeSentence
function capitalizeSentence(str) {
return str.replace(/^\w|(\s\w)/g, function(match, index) {
return match.toUpperCase()
});
}
capitalizeSentence('Lorem ipsum dolor sit amet, consectetur adipiscing elit.');
// "Lorem Ipsum Dolor Sit Amet, Consectetur Adipiscing Elit."
@cms
cms / array-chunks.js
Created March 22, 2019 18:59
array chunks
// Using Array.prototype.reduce:
const getChunks = (array, size) => {
return array.reduce((acc, curr, i) => {
const pos = Math.floor(i/size);
acc[pos] = [...(acc[pos]||[]), curr];
return acc
}, [])
}
// Using Array.from:
@cms
cms / devicons.html
Created February 26, 2019 02:57
DevIcons
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<link rel="stylesheet" href="https://cdn.rawgit.com/konpa/devicon/df6431e323547add1b4cf45992913f15286456d3/devicon.min.css">
<link href='//cdn.jsdelivr.net/devicons/1.8.0/css/devicons.min.css' rel='stylesheet'>
<style>
body {