Skip to content

Instantly share code, notes, and snippets.

View yuripramos's full-sized avatar
currently not coding side-projects

Yuri Pereira Ramos yuripramos

currently not coding side-projects
View GitHub Profile
@yuripramos
yuripramos / all_properties.js
Created September 5, 2016 22:00
Write all functions inside an object
function all_properties(obj)
{
return Object.getOwnPropertyNames(obj);
}
console.log(all_properties(Math));
console.log(all_properties(Array));
@yuripramos
yuripramos / keys_toggle_values.js
Created September 8, 2016 22:07
copy of the object where the keys have become the values and the values the keys
function invert_key_value(obj) {
var result = {};
var keys = _keys(obj);
for (var i = 0, length = keys.length; i < length; i++) {
result[obj[keys[i]]] = keys[i];
}
return result;
}
function _keys(obj)
{
@yuripramos
yuripramos / destructuring.js
Created April 4, 2017 15:40 — forked from mikaelbr/destructuring.js
Several demos and usages for ES6 destructuring. Runnable demos and slides about the same topic: http://git.mikaelb.net/presentations/bartjs/destructuring
// === Arrays
var [a, b] = [1, 2];
console.log(a, b);
//=> 1 2
// Use from functions, only select from pattern
var foo = () => [1, 2, 3];
@yuripramos
yuripramos / scroll.js
Last active July 3, 2017 14:09
scroll.js
function currentYPosition() {
if (self.pageYOffset) return self.pageYOffset;
if (document.documentElement && document.documentElement.scrollTop) return document.documentElement.scrollTop;
if (document.body.scrollTop) return document.body.scrollTop;
return 0;
}
function elmYPosition(eID) {
let elm = document.getElementById(eID);
let y = elm.offsetTop;
// Object destructuring
var someObject = {
a: 1,
b: 2,
c: 3,
d: 4
};
var {a, b, ...remaining} = someObject;
console.log(someObject); // {a: 1, b: 2, c: 3, d: 4} (stays unchanged)
@yuripramos
yuripramos / app.js
Created August 19, 2017 15:42
React pagination PURE
class ItemsApp extends React.Component {
constructor() {
super();
this.state = {
items: ['a','b','c','d','e','f','g','h','i','j','k','2','4','1','343','34','a','b','c','d','e','f','g','h','i','j','k','2','4','1','343','34','a','b','c','d','e','f','g','h','i','j','k','2','4','1','343','34','33'],
currentPage: 1,
itemsPerPage: 30
};
@yuripramos
yuripramos / HOC.js
Created August 22, 2017 20:12 — forked from Restuta/HOC.js
React HOC (Higher Order Component) Example
/* HOC fundamentally is just a function that accepts a Component and returns a Component:
(component) => {return componentOnSteroids; } or just component => componentOnSteroids;
Let's assume we want to wrap our components in another component that is used for debugging purposes,
it just wraps them in a DIV with "debug class on it".
Below ComponentToDebug is a React component.
*/
//HOC using Class
//it's a function that accepts ComponentToDebug and implicitly returns a Class
let DebugComponent = ComponentToDebug => class extends Component {
@yuripramos
yuripramos / Flatten.js
Last active December 10, 2019 10:28
Flatten array without built-in funcs
module.exports = function Flatten(arrays) {
return arrays.reduce((acc, item) => {
const isArray = Array.isArray(item);
const newItems = isArray ? Flatten(item) : [item];
return acc.concat(newItems);
}, [])
}
@yuripramos
yuripramos / bar.js
Last active July 11, 2020 12:08
snippets for medium post "What I’ve learned reading Clean Code and Applying in the front-end environment"
const Bar = props => {
const { data } = props;
const [showButton, setShowButton] = useState();
const parsedData = buildArrayFromGateway(data);
React.useEffect(() => {
if(parsedData) {
setShowButton(true);
} else {
setShowButton(false);
@yuripramos
yuripramos / sideBar.jsx
Last active July 11, 2020 12:09
snippets for medium post "What I’ve learned reading Clean Code and Applying in the front-end environment"
const SideBarContentView = ({ data }) => {
const [showButton, setShowButton] = useState(false);
const parsedData = buildArrayFromGateway(data);
React.useEffect(() => {
parsedData && setShowButton(true);
});
return (
<Wrapper>