Skip to content

Instantly share code, notes, and snippets.

View srph's full-sized avatar
🛠️
Building @Stride-Labs Frontend

Kier Borromeo srph

🛠️
Building @Stride-Labs Frontend
View GitHub Profile
@srph
srph / index.ts
Last active November 18, 2018 22:17
JS: Generate avatar initials from name
export default function(organizationName: string) {
const [f, l]: string[] = organizationName.split(' ')
return [f.charAt(0).toUpperCase(), (l || '').charAt(0).toUpperCase()].join('')
}
@srph
srph / RouterPermission.tsx
Created November 16, 2018 08:55
Reach Router: Advanced permissions proof of concept
import * as React from 'react'
import { Subscribe as UnstatedSubscribe } from 'unstated'
import { AuthContainer } from '@app/containers'
import { TAuthContainerUserData } from '@app/containers/AuthContainer'
import { Redirect } from '@reach/router'
import authOnly from './permission-auth-only'
import guestOnly from './permission-auth-only'
interface IPermissionFunction {
guard: (user: TAuthContainerUserData) => boolean
@srph
srph / index.js
Created October 31, 2018 06:51
Codility: Find the smallest natural number
function solution(A) {
var natural = Array.from(new Array(10000)).map((_, i) => i + 1)
return natural.sort((a, b) => a - b).find((n) => !A.includes(n))
}
console.log(solution([1,2,5]))
@srph
srph / index.js
Created October 16, 2018 05:11
JS: Array-like data structure to array
function obj2arr(obj) {
var arr = []
function flatten (subobj, isRoot) {
var result = {}
Object.keys(subobj).forEach(key => {
if (typeof subobj[key] === 'object') {
flatten(subobj[key], false)
} else {
result[key] = subobj[key]
@srph
srph / index.js
Created October 13, 2018 02:53
JS: Sort by h4 text
x.sort((a, b) => z(a) > z(b))
function z(str) {
return (<h4 style="text-align: center;">)[A-Za-z\,\s]+(<\/h4>)/.exec(str)[0].replace('<h4 style="text-align: center;">', '').replace('</h4>', '').replace(', CA', '')
}
@srph
srph / Tree.json
Last active September 3, 2018 16:31
JS: Flatten tree
{
"id": 1,
"name": "sample 1",
"child": [
{
"id": 12,
"name": "sample 12",
"child": [],
"parentId": 1
},
@srph
srph / index.js
Created September 1, 2018 07:29
JS: Get all 1080 magnet links from HorribleSubs
var els = Array.from(
document.querySelectorAll(".rls-info-container .link-1080p .hs-magnet-link a")
);
var links = ''
for (let i = 0; i < els.length; i++) {
var prefix = i === 0 ? '' : '\n\n'
var magnet = els[i];
links += `${prefix}${magnet.href}`
@srph
srph / chunk-pattern.js
Last active December 18, 2020 14:54
JS: Chunk an array in a pattern (Initial implementation of https://github.com/srph/chunk-pattern)
/* @flow */
/**
* Chunks an array in a specific pattern
* @example chunkPattern(['haha', 'haha', 'haha', 'haha', 'haha', 'haha', 'haha', 'haha', 'haha'], [2, 3])
* @return Array<Array<*>>
*/
function chunkPattern(array: Array<*>, pattern: Array<number>): Array<Array<*>> {
const result: Array<Array<*>> = []
@srph
srph / main.go
Created August 11, 2018 03:48
Go: Decode SOAP XML
package main
import (
"encoding/xml"
"fmt"
)
const data = `<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
@srph
srph / main.go
Created August 10, 2018 22:10
Go: PascalSnake (e.g., Hello_World) un/marshaller
package main
import (
"bytes"
"regexp"
"encoding/json"
)
type PascalSnakeMarshaller struct {
Value interface{}