Skip to content

Commit

Permalink
feat: use @getspectra/spectra-typings
Browse files Browse the repository at this point in the history
  • Loading branch information
palmcivet committed Apr 2, 2024
1 parent 69cecc2 commit be0da83
Show file tree
Hide file tree
Showing 19 changed files with 102 additions and 124 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages

name: Release

on:
Expand Down Expand Up @@ -37,7 +34,7 @@ jobs:
registry-url: https://npm.pkg.github.com/
- run: pnpm publish
env:
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

publish-npm:
needs: build
Expand All @@ -53,4 +50,4 @@ jobs:
registry-url: https://npm.pkg.github.com/
- run: pnpm publish
env:
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
15 changes: 8 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages

name: CI

on: [push, pull_request]
Expand All @@ -9,14 +6,18 @@ jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: write
pages: write
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
- name: Check out repository
uses: actions/checkout@v4

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20

- uses: pnpm/action-setup@v3
- name: Set up pnpm
uses: pnpm/action-setup@v3
with:
version: 8
run_install: |
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Inspired by [《How we built a custom permissions DSL at Figma》](https://www.f
## Installing

```bash
pnpm install spectra-js
pnpm install @getspectra/spectra-js
```

## Usage
Expand All @@ -33,7 +33,7 @@ import {
parseDependences,
ResourceInterface,
DataInterface,
} from 'spectra-js';
} from '@getspectra/spectra-js';

function loadDataFromDatabase(resources: ResourceInterface): DataInterface {
return {
Expand Down
14 changes: 11 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "spectra-js",
"name": "@getspectra/spectra-js",
"version": "0.0.0",
"license": "MIT",
"publishConfig": {
Expand All @@ -14,8 +14,13 @@
"type": "git",
"url": "https://github.com/getspectra/spectra-js"
},
"description": "",
"keywords": [],
"description": "JavaScript implementation of the Spectra DSL.",
"keywords": [
"DSL",
"permissions",
"spectra",
"figma"
],
"type": "module",
"scripts": {
"dev": "jest --watchAll",
Expand All @@ -29,5 +34,8 @@
"rimraf": "^5.0.5",
"ts-jest": "^29.1.2",
"typescript": "^5.2.2"
},
"dependencies": {
"@getspectra/spectra-typings": "^0.0.5"
}
}
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions src/expressions/and.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ExpressionInterface, AndExpressionDefine, FieldName } from '@/types';
import { AndExpressionDefinition, FieldName } from '@getspectra/spectra-typings';
import { ExpressionInterface } from '@/types';

export class AndExpression implements ExpressionInterface {
private expressions: Array<ExpressionInterface>;
Expand All @@ -11,7 +12,7 @@ export class AndExpression implements ExpressionInterface {
return 'AND';
}

public getExpression(): AndExpressionDefine {
public getExpression(): AndExpressionDefinition {
return {
and: this.expressions.map((expression) => expression.getExpression()),
};
Expand Down
9 changes: 4 additions & 5 deletions src/expressions/binary.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import {
ExpressionInterface,
BinaryExpressionDefine,
BinaryExpressionDefinition,
FieldName,
FieldValue,
Operation,
DataInterface,
} from '@/types';
} from '@getspectra/spectra-typings';
import { ExpressionInterface, DataInterface } from '@/types';
import { compareValue, getValueFromKey, isArgumentRef } from '@/utils';

export class BinaryExpression implements ExpressionInterface {
Expand All @@ -23,7 +22,7 @@ export class BinaryExpression implements ExpressionInterface {
return this.operation;
}

public getExpression(): BinaryExpressionDefine {
public getExpression(): BinaryExpressionDefinition {
return [this.left, this.operation, this.right];
}

Expand Down
17 changes: 7 additions & 10 deletions src/expressions/factory.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import {
ExpressionDefine,
ExpressionInterface,
OperationEnum,
FieldName,
FieldValue,
} from '@/types';
import { ExpressionDefinition, FieldName, FieldValue } from '@getspectra/spectra-typings';
import { ExpressionInterface, OperationEnum } from '@/types';
import { normalizeExpression } from '@/utils';
import { BinaryExpression } from './binary';
import { AndExpression } from './and';
import { NotExpression } from './not';
import { OrExpression } from './or';

export function and(
expressions: Array<ExpressionInterface | ExpressionDefine>
expressions: Array<ExpressionInterface | ExpressionDefinition>
): AndExpression {
const normalizedExpressions = expressions.map((expression) =>
normalizeExpression(expression)
Expand All @@ -21,15 +16,17 @@ export function and(
}

export function or(
expressions: Array<ExpressionInterface | ExpressionDefine>
expressions: Array<ExpressionInterface | ExpressionDefinition>
): OrExpression {
const normalizedExpressions = expressions.map((expression) =>
normalizeExpression(expression)
);
return new OrExpression(normalizedExpressions);
}

export function not(expression: ExpressionInterface | ExpressionDefine): NotExpression {
export function not(
expression: ExpressionInterface | ExpressionDefinition
): NotExpression {
const normalizedExpression = normalizeExpression(expression);
return new NotExpression(normalizedExpression);
}
Expand Down
5 changes: 3 additions & 2 deletions src/expressions/not.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ExpressionInterface, FieldName, NotExpressionDefine } from '@/types';
import { FieldName, NotExpressionDefinition } from '@getspectra/spectra-typings';
import { ExpressionInterface } from '@/types';

export class NotExpression implements ExpressionInterface {
private expression: ExpressionInterface;
Expand All @@ -11,7 +12,7 @@ export class NotExpression implements ExpressionInterface {
return 'NOT';
}

public getExpression(): NotExpressionDefine {
public getExpression(): NotExpressionDefinition {
return {
not: this.expression.getExpression(),
};
Expand Down
5 changes: 3 additions & 2 deletions src/expressions/or.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ExpressionInterface, FieldName, OrExpressionDefine } from '@/types';
import { FieldName, OrExpressionDefinition } from '@getspectra/spectra-typings';
import { ExpressionInterface } from '@/types';

export class OrExpression implements ExpressionInterface {
private expressions: Array<ExpressionInterface>;
Expand All @@ -11,7 +12,7 @@ export class OrExpression implements ExpressionInterface {
return 'OR';
}

public getExpression(): OrExpressionDefine {
public getExpression(): OrExpressionDefinition {
return {
or: this.expressions.map((expression) => expression.getExpression()),
};
Expand Down
4 changes: 2 additions & 2 deletions src/types/expression.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ExpressionDefinition, FieldName } from '@getspectra/spectra-typings';
import { DataInterface } from './data-loader';
import { ExpressionDefine, FieldName } from './literal';

export type ExpressionInterface = {
getOperation(): string;
getExpression(): ExpressionDefine;
getExpression(): ExpressionDefinition;
getFields(): Array<FieldName>;
evaluate(data: DataInterface): boolean;
jsonSerialize(): string;
Expand Down
1 change: 0 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ export * from './data-loader';
export * from './expression';
export * from './operation';
export * from './policy';
export * from './literal';
29 changes: 0 additions & 29 deletions src/types/literal.ts

This file was deleted.

6 changes: 0 additions & 6 deletions src/types/operation.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
export type NumberOperation = '=' | '!=' | '<>' | '>' | '>=' | '<' | '<=';

export type ArrayOperation = 'in' | 'nin' | 'not_in';

export type Operation = NumberOperation | ArrayOperation;

export enum OperationEnum {
EQ = '=',
NEQ = '!=',
Expand Down
3 changes: 2 additions & 1 deletion src/utils/compare.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ArrayOperation, Operation, OperationEnum } from '@/types';
import { ArrayOperation, Operation } from '@getspectra/spectra-typings';
import { OperationEnum } from '@/types';

/**
* @description Compare an array with a value based on an operation.
Expand Down
21 changes: 21 additions & 0 deletions src/utils/evaluate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ArgumentValue, FieldName, FieldValue } from '@getspectra/spectra-typings';
import { DataInterface } from '@/types';
import { isArgumentRef } from './expression';

/**
* @description Get the value from the key.
*/
export function getValueFromKey(
data: DataInterface,
key: FieldName | FieldValue
): ArgumentValue {
if (typeof key === 'string') {
return data[key];
}

if (key !== null && isArgumentRef(key)) {
return data[key.ref];
}

return key;
}
Loading

0 comments on commit be0da83

Please sign in to comment.