Skip to content

Commit

Permalink
Merge pull request #1 from hsu-aut/release
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
aljoshakoecher authored Aug 18, 2020
2 parents c4f1c29 + 8745e4a commit a4e9a6a
Show file tree
Hide file tree
Showing 22 changed files with 636 additions and 361 deletions.
17 changes: 0 additions & 17 deletions .babelrc

This file was deleted.

10 changes: 10 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
!**/.eslintrc*
node_modules*
dist
*.svg
*.ico
*.json
.gitignore
*.md
*.log
*.lock
15 changes: 0 additions & 15 deletions .eslintrc

This file was deleted.

23 changes: 23 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

/* eslint-disable no-undef */
module.exports = {
root: true,
parser: '@typescript-eslint/parser', // Specifies the ESLint parser
extends: [
"eslint:recommended", // Uses the recommended rules from the @typescript-eslint/eslint-plugin
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
],
parserOptions: {
ecmaVersion: 2018, // Allows for the parsing of modern ECMAScript features
sourceType: 'module', // Allows for the use of imports
},
rules: {
"semi": ["error","always"],
"indent": ["error", 4],
"prefer-const": ["error",{}],
"max-len": ["error", {code: 140, ignoreComments: true}]
// Place to specify ESLint rules. Can be used to overwrite rules specified from the extended configs
// e.g. "@typescript-eslint/explicit-function-return-type": "off",
},
};
11 changes: 10 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# compiled output
/dist
/tmp
/out-tsc
# Only exists if Bazel was run
/bazel-out
tsconfig.build.tsbuildinfo
tsconfig.tsbuildinfo

# Logs
logs
*.log
Expand Down Expand Up @@ -41,4 +50,4 @@ package-lock.json
yarn.lock

others
.DS_Store
.DS_Store
27 changes: 27 additions & 0 deletions .nycrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"cache": false,
"check-coverage": false,
"extension": [
".ts"
],
"include": [
"**/*.js",
"**/*.ts"
],
"exclude": [
"coverage/**",
"node_modules/**",
"**/*.d.ts",
"**/*.test.ts",
"tests/*",
".eslintrc.js"
],
"sourceMap": true,
"reporter": [
"html",
"text",
"text-summary"
],
"all": true,
"instrument": true
}
42 changes: 42 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Mocha Current File",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"--no-timeouts",
"--colors",
"${file}",
"--require",
"ts-node/register"
],
"console": "integratedTerminal",
"sourceMaps": true,
"internalConsoleOptions": "neverOpen"
},
{
"name": "Attach",
"port": 9229,
"request": "attach",
"skipFiles": [
"<node_internals>/**"
],
"type": "pwa-node"
},
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}\\lib\\sparql-result-converter.js"
}
]
}
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"eslint.workingDirectories": [
{ "mode": "auto" }
],
}
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
![npm](https://img.shields.io/npm/v/sparql-result-converter)
![GitHub](https://img.shields.io/github/license/aljoshakoecher/sparql-result-converter)

# sparql-result-converter
A little utility class that helps you to transform the table-like results that you get from a SPARQL query into a nested JSON tree with a user-defined structure.

# How to use it
## How to use it
Just install as a dependecy via `npm install sparql-result-converter`.
In your code you can then use the converter like this:
```javascript
Expand All @@ -10,11 +13,11 @@ const SparqlResultConverter = require('sparql-result-converter');
const converter = new SparqlResultConverter();

// convert a SPARQL result
const convertedResult = converter.convert(sparqlResult, convertStructure);
const convertedResult = converter.convert(inputArray, mappingDefinitions);
```
You have to pass two parameters to `convert()`:
1) `sparqlResult` are the results you get from your SPARQL query
2) `convertStructure` is an array that describes how your converted result should look like. It looks like this:
1) `inputArray` are the results you get from your SPARQL query
2) `mappingDefinitions` is an array that describes how your converted result should look like. It consists of mapping definitions looking like this:

```javascript
const convertStructure = [
Expand All @@ -34,7 +37,7 @@ When you query Triple stores with their REST API, you get a tabular structure wh
![Example Graph](https://github.com/aljoshakoecher/sparql-result-converter/raw/documentation/images/docu-images/example-graph.png)
There are three pet owners with varying numbers of pets. While Peter has only one pet, mary has three. If we'd like to find all pet owners, their pets and the type of pet(s) they have, we migh send the following query against our triple store:
There are three pet owners with varying numbers of pets. While Peter has only one pet, mary has three. If we'd like to find all pet owners, their pets and the type of pet(s) they have, we might send the following query against our triple store:
```SPARQL
PREFIX ex: <http://example.com#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
Expand Down
51 changes: 22 additions & 29 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
{
"name": "sparql-result-converter",
"version": "1.0.2",
"version": "1.0.3",
"description": "Little utility function to that converts the table-like result of a SPARQL query into a JSON tree with a user-defined structure",
"scripts": {
"clean": "rimraf lib",
"test": "npm run lint && npm run cover",
"test:prod": "cross-env BABEL_ENV=production npm run test",
"test:only": "mocha --require babel-register --require babel-core/register --require babel-polyfill --recursive",
"test:watch": "npm test -- --watch",
"cover": "nyc --check-coverage npm run test:only",
"lint": "eslint src test",
"build": "cross-env BABEL_ENV=production babel src --out-dir lib",
"prepare": "npm run clean && npm run build &&npm run lint && npm run test"
"clean": "rimraf dist",
"build": "tsc --project tsconfig.build.json",
"test": "npm run lint && npm run coverage",
"test:only": "mocha -r ts-node/register tests/**/*.test.ts",
"coverage": "nyc npm run test:only",
"lint": "eslint src tests/**/* --ext .ts",
"prepublishOnly": "npm run lint && npm run coverage && npm run build"
},
"files": [
"lib",
"src"
"dist/**/*"
],
"main": "./lib/sparql-result-converter.js",
"main": "./dist/SparqlResultConverter.js",
"repository": {
"type": "git",
"url": "git+https://github.com/aljoshakoecher/sparql-result-converter.git"
Expand All @@ -38,26 +35,22 @@
},
"homepage": "https://github.com/aljoshakoecher/sparql-result-converter#readme",
"dependencies": {
"json-groupby": "^1.1.0",
"lodash": "^4.17.15"
"lodash.groupby": "^4.6.0",
"lodash.isempty": "^4.4.0"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-eslint": "^10.0.1",
"babel-plugin-add-module-exports": "^1.0.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.6.1",
"babel-preset-minify": "0.3.0",
"@types/chai": "^4.2.11",
"@types/lodash.isempty": "^4.4.6",
"@types/mocha": "^7.0.2",
"@typescript-eslint/eslint-plugin": "^3.3.0",
"@typescript-eslint/parser": "^3.3.0",
"chai": "^4.1.2",
"cross-env": "^5.1.3",
"eslint": "^5.16.0",
"eslint-config-airbnb": "^17.1.0",
"eslint": "^6.8.0",
"eslint-plugin-import": "^2.7.0",
"eslint-plugin-jsx-a11y": "^6.0.2",
"eslint-plugin-react": "^7.4.0",
"mocha": "^6.1.3",
"nyc": "^13.3.0",
"rimraf": "^2.6.2"
"nyc": "^15.1.0",
"rimraf": "^2.6.2",
"ts-node": "^8.10.2",
"typescript": "^3.9.5"
}
}
55 changes: 55 additions & 0 deletions src/ArrayUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
export class ArrayUtil {

/**
* Checks whether or not all entries of the array contain the property that is used for grouping
* @param {*} arrayToCheck
* @param {*} groupingProperty
*/
static allEntriesContainGroupingProperty(arrayToCheck: any[], groupingProperty: string): boolean {
for (let i = 0; i < arrayToCheck.length; i++) {
const element = arrayToCheck[i];
if (!Object.prototype.hasOwnProperty.call(element, groupingProperty)) {
return false;
}
}
return true;
}



/**
* Transforms an array of objects to an array of simple datatypes by extracting every value-property.
* @param {*} sparqlResult Array of objects that contain a value-property
*/
static extractValues(sparqlResult: SparqlResultLine[]): TransformedSparqlResultElement[] {

const outputArray = new Array<TransformedSparqlResultElement>();

// Take every array element and extract all values of all object keys -> flatten the array
sparqlResult.forEach((sparqlResultLine:SparqlResultLine) => {
const objectKeys = Object.keys(sparqlResultLine);

const outputElement = {};
objectKeys.forEach((key) => {
outputElement[key] = sparqlResultLine[key].value;
});
outputArray.push(outputElement);
});

return outputArray;
}


}

export interface SparqlResultLine {
[name: string]: {
type: string,
value: string
}
}

export interface TransformedSparqlResultElement {
[propName: string]: string;
}

Loading

0 comments on commit a4e9a6a

Please sign in to comment.