Skip to content

Instantly share code, notes, and snippets.

View namereva's full-sized avatar

Nikita Reva namereva

View GitHub Profile
@namereva
namereva / colorGenerator.ts
Last active May 9, 2026 03:33
Generate hex from name
function generateColor(name: string): string {
let hash = 0;
for (let i = 0; i < name.length; i++) {
hash = ((hash << 5) - hash + name.charCodeAt(i)) | 0;
}
return `#${(Math.abs(hash) % 0x1000000).toString(16).padStart(6, '0')}`;
}
function animateNumber({
start,
end,
duration,
onChange,
}: {
start: number;
end: number;
onChange: (val: number) => void;
duration?: number;
@namereva
namereva / hoverElements.css
Created December 16, 2019 14:47
change all elements except hovered elements
.wrapper:hover > *:not(:hover) {
color: red;
}
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
word-wrap: normal;
@namereva
namereva / provedPassword.js
Created December 18, 2018 12:16
returns a value if it contains a Latin letter, a number, and a special character
const provedPassword = this.password.match(/(?=.*[0-9])(?=.*[!@#$%^&*])(?=.*[a-z|A-Z])/g);
@namereva
namereva / GenerateDate.js
Created December 13, 2018 14:41
how to use 'map' instead of 'for'
const value = 30;
const newArray = Array.from(Array(value)).map((item, index) => ({
date: new Date(2018, 1, index + 1),
}));
@namereva
namereva / mockSetTimeout.js
Last active November 16, 2018 16:32
example how mock setTimeout
handleTimeout = ({ value }) => {
setTimeout(() => this.props.onClose({ value }), 30000);
};
describe('Example', () => {
it('should call exampleProps with value', () => {
const spy = jest.fn();
const wrapper = shallow(
<Example exampleProps={value: 'test'} />
);
@namereva
namereva / mockMathRandom.js
Last active November 16, 2018 16:33
this method always return 0.5
const mockMath = Object.create(global.Math);
mockMath.random = () => 0.5;
global.Math = mockMath;
@namereva
namereva / TestComponentDidUpdate.js
Last active November 16, 2018 16:31
how testing componentDidUpdate with props
componentDidUpdate(prevProps) {
if (prevProps.propsExample !== this.props.propsExample) {
this.handleExample(this.props.propsExample);
}
}
//test enzyme + jest
describe('componentDidUpdate', () => {
it('should call handleExample', () => {
const value = 'test';