Skip to content

Instantly share code, notes, and snippets.

View cyrilf's full-sized avatar
🌱
what's up!

cyril cyrilf

🌱
what's up!
View GitHub Profile
@cyrilf
cyrilf / generatePassword.js
Created October 18, 2018 14:11
Simple password generator in JS
// Add IE11 support
const crypto = window.crypto || window.msCrypto
const generatePassword = (length = 10) => {
const buffer = new Uint8Array(12)
crypto.getRandomValues(buffer)
return btoa(String.fromCharCode.apply(null, buffer)).substr(0, length)
}
export default generatePassword
@cyrilf
cyrilf / flattenArray.js
Created October 18, 2018 09:57
React util function to find and replace string with links in a label
const flattenArray = (arr) => arr.reduce((result, value) => result.concat(value), [])
@cyrilf
cyrilf / react-select-event.jsx
Created August 30, 2018 15:45
Solution to receive an event with react-select onChange method
// Issue: `react-select` `onChange` method doesn't return a proper event
//
// Solution used:
//
// The main idea is to have an hidden `select` that will trigger a real event
// When `ReactSelect` triggers it's `onChange` we set the state with the `selectedOptions`
// these `selectedOptions` will be what populate our hidden select.
// Using the callback from `setState` (it ensures that the new values are already into our hidden select),
// we then create a change event and dispatch it into our hidden select.
// This will cause the `onChange` from this select to be triggered with a proper `event`.
@cyrilf
cyrilf / proxy.js
Last active June 21, 2018 12:38
Proxy
// From: https://stackoverflow.com/questions/50963215/can-we-specify-a-generic-getter-on-an-object-in-javascript
// Allows you to access nested property (1 level) without the need to to safety check
const properties = { email: { title: "Email", value: "[email protected]" } }
const proxy = new Proxy(properties, {
get: (target, prop) => (prop in target) ? target[prop] : {},
})
// or
// Utility function to get access nested object properties securely
//
// Usage:
// const invoiceName = get(account, "user.invoiceAddress.name")
//
// Instead of:
// const invoiceName = (account && account.user && account.user.invoiceAddress && account.user.invoiceAddress.name) || null
// https://stackoverflow.com/a/23809123/1410020
const get = (obj, key) =>
@cyrilf
cyrilf / hoc.js
Created January 17, 2018 11:46
React custom life-cycle method
// Inspired by https://github.com/MicheleBertoli/react-automata/blob/d9128bebe30df83c41bff4ed806549241fcf3b04/src/withStatechart.js
// Example of how to use a custom life-cycle method for a wrapped component
import React from 'react'
const isStateless = Component =>
!(Component.prototype && Component.prototype.isReactComponent)
const withSomething = Component => {
@cyrilf
cyrilf / includedByState-example.js
Last active August 29, 2015 14:14
includedByState filter improvement
// Improvement proposal
$IncludedByStateFilter.$inject = ['$state'];
function $IncludedByStateFilter($state) {
var includesFilter = function (state, params, options) {
return $state.includes(state, params, options);
};
includesFilter.$stateful = true;
return includesFilter;
}
@cyrilf
cyrilf / ZfcUserZendDb.php
Created July 23, 2013 15:07
BjyAuthorize issue #170
<?php
/**
* BjyAuthorize Module (https://github.com/bjyoungblood/BjyAuthorize)
*
* @link https://github.com/bjyoungblood/BjyAuthorize for the canonical source repository
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace BjyAuthorize\Provider\Identity;
@cyrilf
cyrilf / hasNestedProperties.js
Created June 11, 2013 16:34
Helper functions in JavaScript
/**
* hasNestedProperties check if an object has the nested
* properties passed in params
*
* @param {Object} obj object we want to test
* @param {String} properties nested property we want
* @return {Boolean} either the object has these
* nested properties or not
*/
@cyrilf
cyrilf / saveWAH.js
Created April 12, 2013 01:01
Save your We Are Hunted playlist. Download all your We Are Hunted tracks infos in a JSON file.
// Save your We Are Hunted playlist
// Download all your We Are Hunted tracks infos in a JSON file
// by Cyril F (github.com/cyrilf)
// Instructions
//
// Go to wearehunted.com
// Change the USERNAME below by your username or email.
// Copy and paste the following code into your javascript
// console.