Skip to content

Instantly share code, notes, and snippets.

Avatar
🙂
Writing code

Yair Even Or yairEO

🙂
Writing code
View GitHub Profile
@yairEO
yairEO / ActionCard.jsx
Last active February 15, 2023 09:50
Testing complex redux dispatch
View ActionCard.jsx
const ActionCard = ({id, dataTestId, icon, label, onClick, dispatch}) => {
const onActionCardClick = () => {
dispatch((dispatch, getState) => onClick(dispatch, getState()));
};
return (
<div id={id} data-test-id={dataTestId} className='action-card' onClick={onActionCardClick}>
<Icon type={icon}/>
<Truncate wrapper={(<Highlighter/>)}>{i18next.t(label)}</Truncate>
</div>
@yairEO
yairEO / README.md
Created January 13, 2023 19:44
chai + sinon: testing code with DOM events
View README.md

How to test a React hooks which is binding a global window/document event(s):

This is an easy approach for testing DOM events in hooks/components without actually emmitting/triggering any "fake" events, which is much easier:

  1. In the tests file, create a dummy React component and call the hook. if the hook is returning something, then assign it to a varaible which should be defined from outside the component so it will be available for the tests cases.
@yairEO
yairEO / JSON.parse.js
Created January 5, 2023 09:25
Safe javascript JSON.parse which never throws exceptions
View JSON.parse.js
JSON.parse = (JP => (...args) => {
try { return JP(...args) } catch{}
})(JSON.parse)
@yairEO
yairEO / for.async.js
Created March 19, 2022 14:40
Async iterators
View for.async.js
const delay = ms => new Promise(resolve => setTimeout(resolve, ms))
const inc = async i => (await delay(500), ++i)
const foo = async () => {
for(let i = 1; i <= 5; i = await inc(i))
console.log(i) // prints 1, 2, 3, 4, 5 with a delay
}
foo()
@yairEO
yairEO / conditional-styles.scss
Created February 5, 2022 20:50
Toggle CSS block with a single variable
View conditional-styles.scss
// https://css-tricks.com/css-switch-case-conditions
// simplified version of my method:
.foo {
--feature: 1; // 1 is "on", 0 is "off"
animation: foo_styles 1s calc(-1s * (var(--feature) - 1)) paused;
@keyframes foo_styles {
0% {
@yairEO
yairEO / math.css
Created February 3, 2022 14:31
CSS math
View math.css
/***** ceil ******/
/* For a value between 0 - 1, where 1 is the maximum possible */
--ceil: clamp(0, calc((1 - var(--value)) * 100), 1);
/***** floor ******/
/* For a value between 0 - 1, where 1 is the maximum possible, use a value just a tiny bit below the maximum for the
math to work, so the output will be either positive or negative when magnified by a factor of 999999*/
@yairEO
yairEO / input.scss
Created January 10, 2022 18:33
Generated by SassMeister.com.
View input.scss
div {
transform: translate(100px) var(--transform-y, );
}
@yairEO
yairEO / input.scss
Last active January 10, 2022 18:32
Generated by SassMeister.com.
View input.scss
// styles of an external object which is placed
// next to a node from the outside
.diagram-node__satelite {
transform: translate(100px) var(--transform-y, );
}
@yairEO
yairEO / removeStyleProp.js
Last active December 22, 2021 18:35
Remove a single property from DOM element "style" attribute
View removeStyleProp.js
const removeStyleProp = (elm, prop) =>
elm.style.cssText = elm.style.cssText // cssText automatically (luckily) adds spaces between declarations
.split('; ')
.filter(p => !p.startsWith(prop) )
.join(';');
@yairEO
yairEO / concatWithoutDups.js
Created November 6, 2021 18:58
Concatenates N arrays without dupplications
View concatWithoutDups.js
/**
* Concatenates N arrays without dups.
* If an array's item is an Object, compare by `value`
* @param {*} k
*/
export const concatWithoutDups = (...key) => {
const result = (...args) => {
const newArr = [],
existingObj = {};