Skip to content

Instantly share code, notes, and snippets.

View bezenson's full-sized avatar
💻

Vladislav Bezenson bezenson

💻
  • Warsaw
View GitHub Profile
@bezenson
bezenson / validate_init_data.js
Created January 21, 2023 22:25 — forked from zubiden/validate_init_data.js
Telegram Web Bots data validation in JavaScript via Web Crypto API (dependency-free)
// Thanks to @MarvinMiles for Telegram Widget Login check function https://gist.github.com/MarvinMiles/f041205d872b0d8547d054eafeafe2a5
// This function validates Web App input https://core.telegram.org/bots/webapps#validating-data-received-via-the-web-app
// Transforms Telegram.WebApp.initData string into object
function transformInitData(initData) {
return Object.fromEntries(new URLSearchParams(initData));
}
// Accepts init data object and bot token
async function validate(data, botToken) {
// Like `anyPass`, but return predicate result, not boolean.
import { curry, curryN, isNil, max, pluck, reduce } from 'ramda';
export const anyPassValue = curry(preds =>
curryN(reduce(max, 0, pluck('length', preds)), (...args) => {
let idx = 0;
const len = preds.length;
while (idx < len) {
const callResult = preds[idx].apply(this, args);
if (!isNil(callResult)) {
@bezenson
bezenson / 01_uniqWithByFields.js
Last active August 25, 2020 22:19
Ramda uniqWith by multiple keys in object
import { allPass, curry, eqProps, map, uniqWith } from 'ramda';
const uniqWithByFields = curry((fields, data) => uniqWith(
allPass(
map(eqProps)(fields)
),
)(data));
@bezenson
bezenson / sortListByPattern.js
Created August 17, 2018 08:58
Sort array by original array
import { curry, pipe, sort } from 'ramda';
const sortListByPattern = curry((pattern, list) => pipe(
sort((a, b) => pattern.indexOf(a) - pattern.indexOf(b)),
)(list));
const pattern = ['s', 'o', 'm', 'e', 'v', 'a', 'l'];
const list = ['o', 'a', 'e', 'v', 'l', 's'];
@bezenson
bezenson / toggleListItem.js
Last active October 2, 2020 06:51
Toggle item in array with ramda
import { append, contains, curry, ifElse, without } from 'ramda';
const toggleListItem = curry((value, list) => ifElse(
contains(value),
without([value]),
append(value),
)(list));
// Example:
const data = ['a', 'b', 'c', 'd'];
<div id="swipezone">
Swipe me
</div>
// credit: http://www.javascriptkit.com/javatutors/touchevents2.shtml
function swipedetect(el, callback){
var touchsurface = el,
swipedir,
startX,
{
"codes": [
{
"name": "elen01",
"code": "elen01",
"image": false,
"image_mobile_stock": false,
"image_mobile_shop": false
},
{
@bezenson
bezenson / scrollTo.js
Last active September 15, 2017 14:07
/* eslint-disable no-param-reassign */
import raf from 'rafl';
import { Tween, Easing } from '@tweenjs/tween.js';
function noop() {}
export default function scrollTo(element, options) {
let { duration, onComplete } = options;
const { left, top } = options;
this.point = document.createElement('div');
document.body.appendChild(this.point);
this.point.style.width = '10px';
this.point.style.height = '10px';
this.point.style.backgroundColor = 'red';
this.point.style.borderRadius = '50%';
this.point.style.position = 'absolute';
this.point.style.zIndex = '999999';
this.point.style.left = '0px';
this.point.style.top = '0px';
@bezenson
bezenson / README.MD
Last active September 8, 2017 15:45
Move PropTypes from 'react' to 'prop-types' library fast.

If you upgraded react to latest version and got a warning Accessing PropTypes via the main React package is deprecated, and will be removed in React v16.0., but you import PropTypes from react package all over your project then check 3 simple regular expressions to find and replace in your project.

Find:

  1. (import.+)(, \{ PropTypes \})(.+'react';)$
  2. (import.+)(PropTypes, )(.+'react';)$
  3. (import.+)(, PropTypes)(.+'react';)$

Replace: