From e0318958abf1f44a7a76f677227216ce9b21db55 Mon Sep 17 00:00:00 2001 From: Hubert Lesiak Date: Sun, 10 May 2020 19:34:27 +0200 Subject: [PATCH] v0.0.2 --- .gitignore | 20 ++-- README.MD | 94 +++++++++--------- src/mod.ts | 284 ++++++++++++++++++++++++++--------------------------- 3 files changed, 199 insertions(+), 199 deletions(-) diff --git a/.gitignore b/.gitignore index 548812f..d6b7571 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,10 @@ - -.vscode/* -!.vscode/settings.json -!.vscode/tasks.json -!.vscode/launch.json -!.vscode/extensions.json -*.code-workspace - -# Local History for Visual Studio Code -.history/ + +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +*.code-workspace + +# Local History for Visual Studio Code +.history/ diff --git a/README.MD b/README.MD index 7de4106..dd5cc34 100644 --- a/README.MD +++ b/README.MD @@ -1,47 +1,47 @@ -## 🥑 What? - -AQL Template Tag Literal. -Selfcontaing ES Module with no dependencies. - -## 🥑 Why? - -To work with AQL Queries anywhere. - -## 🥑 Usage - -```ts -import { aql } from "https://raw.githubusercontent.com/Tnifey/aql-tag/master/src/mod.ts"; -``` - -### example - -Works like [AQL Helpers](https://www.arangodb.com/docs/stable/drivers/js-reference-aql.html). - -TL;DR - -```ts -// import { aql } from '...' - -const arr = [1, 2, 3]; - -const query = aql`for u in ${arr}`; -``` - -_query returns_ - -```ts -{ - query: "for u in @value0", - bindVars: { value0: [1,2,3] }, -} -``` - -## 🥑 License - -Apache License 2.0 - -Slightly modified by [Tnifey](https://github.com/Tnifey) - -Source from: [https://github.com/arangodb/arangojs/blob/v6/src/aql-query.ts] - -🥑 +## 🥑 What? + +AQL Template Tag Literal. +Selfcontaing ES Module with no dependencies. + +## 🥑 Why? + +To work with AQL Queries anywhere. + +## 🥑 Usage + +```ts +import { aql } from "https://raw.githubusercontent.com/Tnifey/aql-tag/master/src/mod.ts"; +``` + +### example + +Works like [AQL Helpers](https://www.arangodb.com/docs/stable/drivers/js-reference-aql.html). + +TL;DR + +```ts +// import { aql } from '...' + +const arr = [1, 2, 3]; + +const query = aql`for u in ${arr}`; +``` + +_query returns_ + +```ts +{ + query: "for u in @value0", + bindVars: { value0: [1,2,3] }, +} +``` + +## 🥑 License + +Apache License 2.0 + +Slightly modified by [Tnifey](https://github.com/Tnifey) + +Source from: [https://github.com/arangodb/arangojs/blob/v6/src/aql-query.ts] + +🥑 diff --git a/src/mod.ts b/src/mod.ts index 6cd3e8c..366b9fc 100644 --- a/src/mod.ts +++ b/src/mod.ts @@ -1,142 +1,142 @@ -// source from: https://github.com/arangodb/arangojs/blob/v6/src/aql-query.ts - -export function aql( - templateStrings: TemplateStringsArray, - ...args: AqlValue[] -): GeneratedAqlQuery { - const strings: string[] = [...templateStrings]; - const values: Map = new Map(); - - let query: string = strings[0]; - - for (let i = 0; i < args.length; i++) { - const current = args[i]; - const currentString = strings[i]; - const nextString = strings[i + 1]; - - if (current === undefined) { - query += nextString; - continue; - } - - if (isGeneratedAqlQuery(current)) { - const source = current._source(); - - if (source.args.length) { - query += source.strings[0]; - args.splice(i, 1, ...source.args); - strings.splice( - i, - 2, - currentString + source.strings[0], - ...source.strings.slice(1, source.args.length), - source.strings[source.args.length] + nextString, - ); - } else { - query += current.query + nextString; - args.splice(i, 1); - strings.splice(i, 2, currentString + current.query + nextString); - } - - i -= 1; - continue; - } - - if (isAqlLiteral(current)) { - query += `${current.toAQL()}${nextString}`; - continue; - } - - const name = values.get(current) ?? `value${values.size}`; - - if (isArangoCollection(current)) { - values.set(current.name, `@${name}`); - query += `@@${name}`; - query += nextString; - continue; - } - - if (!values.has(current)) { - values.set(current, name); - } - - query += `@${name}`; - query += nextString; - } - - const bindVars = Object.fromEntries([...values].map(([v, n]) => [n, v])); - return { query, bindVars, _source: () => ({ strings, args }) }; -} - -export namespace aql { - export function literal(value: AqlLiteralValueType): AqlLiteral { - if (isAqlLiteral(value)) return value; - return { toAQL: () => String(value ?? "") }; - } - - export function join( - values: AqlValue[], - separator: string = " ", - ): GeneratedAqlQuery { - if (!values.length) return aql``; - if (values.length === 1) return aql`${values}`; - - return aql( - ["", ...Array(values.length - 1).fill(separator), ""] as any, - ...values, - ); - } -} - -// Helpers - -export function isArangoCollection(value: any): value is ArangoCollection { - return Boolean(value?.isArangoCollection); -} - -export function isGeneratedAqlQuery(value: any): value is GeneratedAqlQuery { - return typeof value?._source === "function"; -} - -export function isAqlLiteral(value: any): value is AqlLiteral { - return typeof value?.toAQL === "function"; -} - -// Types - -export interface ArangoCollection { - isArangoCollection: true; - name: string; -} - -export interface AqlQuery { - query: string; - bindVars: { [name: string]: any; }; -} - -export interface GeneratedAqlQuery extends AqlQuery { - _source: () => { strings: string[]; args: any[]; }; -} - -export interface AqlLiteral { - toAQL: () => string; -} - -export type AqlLiteralValueType = - | string - | number - | AqlLiteral - | null - | undefined; - -export type AqlValue = - | ArangoCollection - | GeneratedAqlQuery - | AqlLiteral - | string - | number - | boolean - | null - | undefined - | object - | any[]; +// source from: https://github.com/arangodb/arangojs/blob/v6/src/aql-query.ts + +export function aql( + templateStrings: TemplateStringsArray, + ...args: AqlValue[] +): GeneratedAqlQuery { + const strings: string[] = [...templateStrings]; + const values: Map = new Map(); + + let query: string = strings[0]; + + for (let i = 0; i < args.length; i++) { + const current = args[i]; + const currentString = strings[i]; + const nextString = strings[i + 1]; + + if (current === undefined) { + query += nextString; + continue; + } + + if (isGeneratedAqlQuery(current)) { + const source = current._source(); + + if (source.args.length) { + query += source.strings[0]; + args.splice(i, 1, ...source.args); + strings.splice( + i, + 2, + currentString + source.strings[0], + ...source.strings.slice(1, source.args.length), + source.strings[source.args.length] + nextString, + ); + } else { + query += current.query + nextString; + args.splice(i, 1); + strings.splice(i, 2, currentString + current.query + nextString); + } + + i -= 1; + continue; + } + + if (isAqlLiteral(current)) { + query += `${current.toAQL()}${nextString}`; + continue; + } + + const name = values.get(current) ?? `value${values.size}`; + + if (isArangoCollection(current)) { + values.set(current.name, `@${name}`); + query += `@@${name}`; + query += nextString; + continue; + } + + if (!values.has(current)) { + values.set(current, name); + } + + query += `@${name}`; + query += nextString; + } + + const bindVars = Object.fromEntries([...values].map(([v, n]) => [n, v])); + return { query, bindVars, _source: () => ({ strings, args }) }; +} + +export namespace aql { + export function literal(value: AqlLiteralValueType): AqlLiteral { + if (isAqlLiteral(value)) return value; + return { toAQL: () => String(value ?? "") }; + } + + export function join( + values: AqlValue[], + separator: string = " ", + ): GeneratedAqlQuery { + if (!values.length) return aql``; + if (values.length === 1) return aql`${values}`; + + return aql( + ["", ...Array(values.length - 1).fill(separator), ""] as any, + ...values, + ); + } +} + +// Helpers + +export function isArangoCollection(value: any): value is ArangoCollection { + return Boolean(value?.isArangoCollection); +} + +export function isGeneratedAqlQuery(value: any): value is GeneratedAqlQuery { + return typeof value?._source === "function"; +} + +export function isAqlLiteral(value: any): value is AqlLiteral { + return typeof value?.toAQL === "function"; +} + +// Types + +export interface ArangoCollection { + isArangoCollection: true; + name: string; +} + +export interface AqlQuery { + query: string; + bindVars: { [name: string]: any; }; +} + +export interface GeneratedAqlQuery extends AqlQuery { + _source: () => { strings: string[]; args: any[]; }; +} + +export interface AqlLiteral { + toAQL: () => string; +} + +export type AqlLiteralValueType = + | string + | number + | AqlLiteral + | null + | undefined; + +export type AqlValue = + | ArangoCollection + | GeneratedAqlQuery + | AqlLiteral + | string + | number + | boolean + | null + | undefined + | object + | any[];