Skip to content

Instantly share code, notes, and snippets.

View langpavel's full-sized avatar

Pavel Lang langpavel

View GitHub Profile
@langpavel
langpavel / GraphQLTimestamp.js
Last active July 28, 2023 08:46
GraphQLTimestamp.js
import { Kind } from 'graphql/language';
import { GraphQLScalarType } from 'graphql';
function serializeDate(value) {
if (value instanceof Date) {
return value.getTime();
} else if (typeof value === 'number') {
return Math.trunc(value);
} else if (typeof value === 'string') {
return Date.parse(value);
@langpavel
langpavel / http-status.ts
Created May 20, 2022 13:29
Type definitions for HTTP status codes
/* eslint-disable @typescript-eslint/no-redeclare */
export function isHttpStatusSuccess(code: number): code is HTTP_STATUS_SUCCESS {
return code >= 200 && code < 300;
}
export function isHttpStatusClientError(code: number): code is HTTP_STATUS_CLIENT_ERROR {
return code >= 400 && code < 500;
}
export function isHttpStatusServerError(code: number): code is HTTP_STATUS_SERVER_ERROR {
@langpavel
langpavel / cssRulesIterator.js
Created April 12, 2022 11:12
filter all CSS rules
function* cssRulesIterator() {
for(const stylesheet of window.document.styleSheets) {
for (const rule of stylesheet.cssRules) {
yield rule.cssText;
}
}
}
[...cssRulesIterator()].filter(ruleText => true)
@langpavel
langpavel / uaccent.sql
Last active July 12, 2021 10:18
PostgreSQL URL slug usable for indices
/**
* original unaccent function isn't usable for indices
*/
CREATE OR REPLACE FUNCTION uaccent(text) RETURNS text AS
$uaccent$
SELECT unaccent('unaccent',$1::text);
$uaccent$ LANGUAGE sql IMMUTABLE;
@langpavel
langpavel / mergeExtensionsIntoAST.js
Last active December 4, 2020 15:37
GraphQL: Merge Extensions Into AST
const invariant = require('invariant');
const { Kind } = require('graphql');
const byKindGetInfo = {
// SchemaDefinition
[Kind.SCHEMA_DEFINITION]: def => ({
isExtension: false,
type: 'schema',
typeName: 'schema',
}),
@langpavel
langpavel / graphql-ast.ts
Created December 1, 2020 17:10
GraphQL AST Type Definitions converted to data structures from `graphql/language/ast.d.ts`
/**
* Types as data from 'graphql/language/ast.d.ts'
*/
import type { ASTNode } from 'graphql/language/ast';
export interface ASTNodeFieldInfo {
readonly optional?: boolean;
readonly isArray?: boolean;
readonly type: string;
@langpavel
langpavel / create-comparator.ts
Created May 29, 2019 22:37
TypeScript comparator factory for Array.sort()
// Usage:
const candidates: any[] = [];
// ...
candidates.sort(createComparator(
(x) => x.ambiguous,
(_, y) => y.refCount, // DESC
(x) => x.name.length,
(x) => x.name,
));
@langpavel
langpavel / generate-urls.d.ts
Created April 10, 2019 09:43
universal-router generated type definitions
import { Params } from './types';
import UniversalRouter from './universal-router';
export default function generateUrls(router: UniversalRouter<any, any>, options?: any): (routeName: string, params: Params) => string;
//# sourceMappingURL=generate-urls.d.ts.map
@langpavel
langpavel / graphql-loader-ts.js
Created April 8, 2019 15:14
graphql-loader-ts (really not finished)
/* eslint-disable max-len, consistent-return, no-continue, no-restricted-syntax, func-names, no-throw-literal, global-require */
// really heavily borrowed from 'graphql-tag/loader'
const fs = require('fs');
const gql = require('graphql-tag/src');
const { addTypenameToDocument } = require('apollo-utilities');
// Takes `lines` (the source GraphQL query string)
// and `doc` (the parsed GraphQL document) and tacks on
// the imported definitions.

💯 🔢 👍 👎 🥇 🥈 🥉 🎱 🅰️ 🆎