Skip to content

Instantly share code, notes, and snippets.

View varunpvp's full-sized avatar
🎯
Focusing

Varun Pujari varunpvp

🎯
Focusing
View GitHub Profile
@varunpvp
varunpvp / filterListBySearch.ts
Created August 22, 2022 12:06
A function to search a list of object with multiple properties
function filterListBySearch<T>(
list: T[],
props: Array<keyof T>,
searchText: string
) {
const regex = new RegExp(searchText, 'i')
return list.filter(it => props.some(prop => regex.test(`${it[prop]}`)))
}
@varunpvp
varunpvp / formatSeconds.ts
Created August 22, 2022 12:04
A function to convert seconds in to human readable text
const formatSeconds = (seconds: number): string => {
const values = [24 * 60 * 60, 60 * 60, 60, 1]
const units = [
['day', 'days'],
['hour', 'hours'],
['minute', 'minutes'],
['second', 'seconds']
]
const parts = []
function areBracketsBalanced(s) {
const opening = ['{', '[', '(']
const closing = ['}', ']', ')']
const stack = []
const closingFor = {
'{': '}',
'[': ']',
'(': ')',
}
@varunpvp
varunpvp / functional-typescript.ts
Last active May 19, 2021 03:22
TypeScript functional programming core
function max(x: number, y: number): number {
return x > y ? x : y;
}
function min(x: number, y: number): number {
return x < y ? x : y;
}
function sum(list: number[]): number {
if (len(list) === 0) {
@varunpvp
varunpvp / flutter-random-chess-app.dart
Last active February 9, 2021 07:33
Flutter Random Chess App home
import 'package:flutter/material.dart';
import 'package:flutter_stateless_chessboard/flutter_stateless_chessboard.dart';
import 'utils.dart';
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
@varunpvp
varunpvp / flutter-random-chess-app-main.dart
Created February 9, 2021 07:30
Flutter Random Chess App main
import 'package:flutter/material.dart';
import 'home_page.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@varunpvp
varunpvp / flutter-random-chess-app-1.dart
Last active February 9, 2021 07:15
Flutter Random Chess App 1
import 'package:flutter/material.dart';
import 'package:flutter_stateless_chessboard/flutter_stateless_chessboard.dart';
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
String _fen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1';
@varunpvp
varunpvp / utils.dart
Created February 9, 2021 07:08
Flutter Random Chess App utils
import 'package:chess/chess.dart' as ch;
String makeMove(String fen, dynamic move) {
final chess = ch.Chess.fromFEN(fen);
if (chess.move(move)) {
return chess.fen;
}
return null;
@varunpvp
varunpvp / ChessUtilsMakeMove.js
Last active January 10, 2021 11:42
Chess utils make move
describe("makeMove", () => {
test("should return fen and full move", () => {
expect(
makeMove(
"rnbqkbnr/pppp1ppp/8/4p3/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2",
"Bc5"
)
).toEqual({
fen: "rnbqk1nr/pppp1ppp/8/2b1p3/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 2 3",
fullMove: {
const { getSideToPlayFromFen } = require("./utils");
describe("getSideToPlayFromFen", () => {
test("should return white", () => {
expect(
getSideToPlayFromFen(
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
)
).toBe("w");
});