-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
753 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
name: JavaScript json-transform Test | ||
|
||
on: | ||
pull_request: | ||
branches: | ||
- main | ||
paths: | ||
- javascript/json-transform/** | ||
|
||
# cancel previous tests if new commit is pushed to PR branch | ||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} | ||
cancel-in-progress: true | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- uses: actions/setup-node@v4 | ||
with: | ||
node-version: 18 | ||
cache: npm | ||
cache-dependency-path: ./javascript/json-transform/package-lock.json | ||
|
||
- name: Install dependencies | ||
working-directory: ./javascript/json-transform | ||
run: npm ci | ||
|
||
- name: Run tests | ||
working-directory: ./javascript/json-transform | ||
run: npm test |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import FunctionContext from "./functions/common/FunctionContext"; | ||
import Sequence, { asSequence, emptySequence } from "sequency"; | ||
|
||
class JsonElementStreamer { | ||
private readonly context: FunctionContext; | ||
private readonly transformed: boolean; | ||
private readonly value?: any[]; | ||
private readonly _stream?: Sequence<any>; | ||
|
||
private constructor(context: FunctionContext, value: any[] | Sequence<any>, transformed: boolean) { | ||
this.context = context; | ||
this.transformed = transformed; | ||
if (Array.isArray(value)) { | ||
this.value = value; | ||
this._stream = undefined; | ||
} else { | ||
this.value = undefined; | ||
this._stream = value; | ||
} | ||
} | ||
|
||
public knownAsEmpty() { | ||
return this.value && this.value.length == 0; | ||
} | ||
|
||
public stream(skip: number = 0, limit: number = -1) { | ||
if (this._stream != null) { | ||
const skipped = skip > 0 ? this._stream.drop(skip) : this._stream; | ||
return limit > -1 ? skipped.take(limit) : skipped; | ||
} | ||
if (this.value == null) { | ||
return emptySequence(); | ||
} | ||
let valueStream = asSequence(this.value); | ||
if (skip > 0) { | ||
valueStream = valueStream.drop(skip); | ||
} | ||
if (limit > -1) { | ||
valueStream = valueStream.take(limit); | ||
} | ||
if (!this.transformed) { | ||
valueStream = valueStream.map(el => this.context.transform(el)); | ||
} | ||
return valueStream; | ||
} | ||
|
||
public static fromJsonArray(context: FunctionContext, value: any[], transformed: boolean) { | ||
return new JsonElementStreamer(context, value, transformed); | ||
} | ||
|
||
public static fromTransformedStream(context: FunctionContext, stream: Sequence<any>) { | ||
return new JsonElementStreamer(context, stream, true); | ||
} | ||
|
||
public toJsonArray() { | ||
if (this.value) { | ||
return this.value; | ||
} | ||
const ja: any[] = []; | ||
if (this._stream) { | ||
this._stream.forEach(item => ja.push(item)); | ||
} | ||
return ja; | ||
} | ||
} | ||
|
||
export default JsonElementStreamer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.