Skip to content

Instantly share code, notes, and snippets.

View dannysofftie's full-sized avatar
A notable part of my job is unzipping things that aren't intended to be unzipped

Daniel Kimani dannysofftie

A notable part of my job is unzipping things that aren't intended to be unzipped
View GitHub Profile
@dannysofftie
dannysofftie / minio-bucket-name-validator.js
Last active September 7, 2021 15:58
Validate Minio Bucket names.
function isValidBucketName(bucketName) {
if (typeof bucketName !== 'string') return false
// bucket length should be less than and no more than 63
// characters long.
if (bucketName.length < 3 || bucketName.length > 63) {
return false
}
// bucket with successive periods is invalid.
if (bucketName.indexOf('..') > -1) {
let unirest = require('unirest');
let req = unirest('POST', 'https://sandbox.safaricom.co.ke/mpesa/b2c/v1/paymentrequest')
.headers({
'Content-Type': 'application/json',
'Authorization': 'Bearer ojS0BpE4PVvOIQeqftIaCJg6sRrp'
})
.send(JSON.stringify({
"InitiatorName": "testapi",
"SecurityCredential": "hXqyG2If6W9+IU4/stlOT97+9MI8cNbl8VY1Fj1RlSr+tPvUMtCUnWkuQUAgISE02n+7HlDJiYLs1GzAcN7erax/RTqrjA5PThSGP7OY0TzN4zB1cwd+eQ4gf/Y1SPK4ekfsl7Ox7MXYQw1h/ULiIMEZNRKCAIklj3YhcgnzKX9FsKL0XWZ00MZSChP8a4w2ARwZN5L8nz7vSP5yD4ZWt2/RCSIPVAdlEFOYHUVC5fkU8+I+8KCOfqw+PCHsLLag/evP/Ptqpjicbp5q2sj9tdCIqyy9ZwovTLb/BEqP2MWP9iBUg1FpfRPlOrB5sGrZJGmEPpRUw8HT6PhB8husRw==",
"CommandID": "BusinessPayment",

Use this hook to watch element intersection

export const useIntersectionObserver = ({ root, target, onIntersect, threshold = 0.4, rootMargin = '0px', enabled = null }) => {
  useEffect(() => {
    if (!enabled) {
      return;
    }
    const observer = new IntersectionObserver((entries) => entries.forEach((entry) => entry.isIntersecting && onIntersect()), {
      root: root?.current,
      rootMargin,
@dannysofftie
dannysofftie / javascript-form-data.js
Created October 18, 2018 08:28
Retrieve form data into a JavaScript object for sending to server using Ajax or fetch, including multiple checkboxes, multiple select options, multiple radio buttons, and multiple file uploads.
/**
* extracts form data, including checkboxes, multiple select options,
* multiple file uploads,
* and returns an iterable
* @param {HTMLFormElement} form form to extract data
*/
function extractFormData(form) {
if (typeof form == 'undefined')
throw new Error('Requires a form to iterate')
else {
@dannysofftie
dannysofftie / tickles-embed.js
Last active August 10, 2018 07:56
Embed code for Tickles Ad Server.
<script>
(function () {
var script = document.getElementsByTagName('script')[0]
var t = document.createElement('script')
t.src = 'https://adxserver.herokuapp.com/resources/js/tickles-main.js'
t.async = true
t.defer = true
script.parentNode.insertBefore(t, script)
window.addEventListener('load', function () {
// You will paste this code snippet at the bottom of every page you want
@dannysofftie
dannysofftie / server.js
Last active March 29, 2018 11:49
Nodejs server without express or any framework. Handle GET and POST
/*
*I will share how you can achieve the below:
*Handle GET and POST requests in Nodejs without using a framework.
*Serve static files from a public folder (css, js, images).
*/
const http = require('http')
const fs = require("fs")
const path = require("path")
const port = process.env.PORT || 4000
http.createServer((req, res) => {