-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add @segment/analytics-generic-utils (#974)
- Loading branch information
Showing
18 changed files
with
176 additions
and
23 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 |
---|---|---|
|
@@ -98,6 +98,21 @@ steps: | |
key: "v1.1-cache-dev-{{ checksum 'yarn.lock' }}" | ||
paths: ['.yarn/cache/'] | ||
|
||
- label: '[Generic Utils] Lint + Test' | ||
agents: | ||
queue: v1 | ||
commands: | ||
- npm config set "//registry.npmjs.org/:_authToken" $${NPM_TOKEN} | ||
- echo "--- Install dependencies" | ||
- PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 HUSKY=0 yarn install --immutable | ||
- echo "+++ Run QA" | ||
- yarn turbo run --filter=analytics-generic-utils lint | ||
- yarn turbo run --filter=analytics-generic-utils test | ||
plugins: | ||
- ssh://[email protected]/segmentio/cache-buildkite-plugin#v2.0.0: | ||
key: "v1.1-cache-dev-{{ checksum 'yarn.lock' }}" | ||
paths: ['.yarn/cache/'] | ||
|
||
- label: '[Consent] Lint + Test' | ||
agents: | ||
queue: v1 | ||
|
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,6 @@ | ||
--- | ||
'@segment/analytics-generic-utils': major | ||
'@segment/analytics-node': patch | ||
--- | ||
|
||
Refactor to get createDeferred from @segment/analytics-generic-utils lib |
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,4 @@ | ||
/** @type { import('eslint').Linter.Config } */ | ||
module.exports = { | ||
extends: ['../../.eslintrc.isomorphic'], | ||
} |
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 @@ | ||
module.exports = require("@internal/config").lintStagedConfig |
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,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright © 2023 Segment | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
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,3 @@ | ||
# @segment/analytics-generic-utils | ||
|
||
This monorepo's version of "lodash". This package contains shared generic utilities that can be used within the ecosystem. This package should not have dependencies, and should not contain any references to the Analytics domain. |
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,3 @@ | ||
const { createJestTSConfig } = require('@internal/config') | ||
|
||
module.exports = createJestTSConfig(__dirname) |
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,36 @@ | ||
{ | ||
"name": "@segment/analytics-generic-utils", | ||
"version": "0.0.1", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/segmentio/analytics-next", | ||
"directory": "packages/generic-utils" | ||
}, | ||
"license": "MIT", | ||
"main": "./dist/cjs/index.js", | ||
"module": "./dist/esm/index.js", | ||
"types": "./dist/types/index.d.ts", | ||
"files": [ | ||
"dist/", | ||
"src/", | ||
"!**/__tests__/**", | ||
"!*.tsbuildinfo", | ||
"LICENSE" | ||
], | ||
"sideEffects": false, | ||
"scripts": { | ||
".": "yarn run -T turbo run --filter=@segment/analytics-generic-utils", | ||
"test": "yarn jest", | ||
"lint": "yarn concurrently 'yarn:eslint .' 'yarn:tsc --noEmit'", | ||
"build": "yarn concurrently 'yarn:build:*'", | ||
"build:esm": "yarn tsc -p tsconfig.build.json", | ||
"build:cjs": "yarn tsc -p tsconfig.build.json --outDir ./dist/cjs --module commonjs", | ||
"watch": "yarn build:esm --watch", | ||
"watch:test": "yarn test --watch", | ||
"tsc": "yarn run -T tsc", | ||
"eslint": "yarn run -T eslint", | ||
"concurrently": "yarn run -T concurrently", | ||
"jest": "yarn run -T jest" | ||
}, | ||
"packageManager": "[email protected]" | ||
} |
39 changes: 39 additions & 0 deletions
39
packages/generic-utils/src/create-deferred/__tests__/create-deferred.test.ts
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,39 @@ | ||
import { createDeferred } from '../create-deferred' | ||
|
||
describe(createDeferred, () => { | ||
it('should return a deferred object', async () => { | ||
const o = createDeferred() | ||
expect(o.promise).toBeInstanceOf(Promise) | ||
expect(o.resolve).toBeInstanceOf(Function) | ||
expect(o.reject).toBeInstanceOf(Function) | ||
}) | ||
it('should resolve', async () => { | ||
const { promise, resolve } = createDeferred<string>() | ||
let isResolved = false | ||
let isResolvedVal = undefined | ||
void promise.then((value) => { | ||
isResolvedVal = value | ||
isResolved = true | ||
}) | ||
expect(isResolved).toBe(false) | ||
expect(isResolvedVal).toBeUndefined() | ||
await resolve('foo') | ||
expect(isResolved).toBe(true) | ||
expect(isResolvedVal).toBe('foo') | ||
}) | ||
|
||
it('should reject', async () => { | ||
const { promise, reject } = createDeferred<string>() | ||
let isRejected = false | ||
let isRejectedVal = undefined | ||
void promise.catch((value) => { | ||
isRejectedVal = value | ||
isRejected = true | ||
}) | ||
expect(isRejected).toBe(false) | ||
expect(isRejectedVal).toBeUndefined() | ||
await reject('foo') | ||
expect(isRejected).toBe(true) | ||
expect(isRejectedVal).toBe('foo') | ||
}) | ||
}) |
16 changes: 16 additions & 0 deletions
16
packages/generic-utils/src/create-deferred/create-deferred.ts
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,16 @@ | ||
/** | ||
* Return a promise that can be externally resolved | ||
*/ | ||
export const createDeferred = <T>() => { | ||
let resolve!: (value: T | PromiseLike<T>) => void | ||
let reject!: (reason: any) => void | ||
const promise = new Promise<T>((_resolve, _reject) => { | ||
resolve = _resolve | ||
reject = _reject | ||
}) | ||
return { | ||
resolve, | ||
reject, | ||
promise, | ||
} | ||
} |
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 @@ | ||
export * from './create-deferred' |
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 @@ | ||
export * from './create-deferred' |
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,9 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"include": ["src"], | ||
"exclude": ["**/__tests__/**", "**/*.test.*"], | ||
"compilerOptions": { | ||
"outDir": "./dist/esm", | ||
"declarationDir": "./dist/types" | ||
} | ||
} |
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,11 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"exclude": ["node_modules", "dist"], | ||
"compilerOptions": { | ||
"module": "esnext", | ||
"target": "ES5", | ||
"moduleResolution": "node", | ||
"resolveJsonModule": true, | ||
"lib": ["es2020"] | ||
} | ||
} |
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 was deleted.
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 |
---|---|---|
|
@@ -3747,6 +3747,12 @@ __metadata: | |
languageName: unknown | ||
linkType: soft | ||
|
||
"@segment/[email protected], @segment/analytics-generic-utils@workspace:packages/generic-utils": | ||
version: 0.0.0-use.local | ||
resolution: "@segment/analytics-generic-utils@workspace:packages/generic-utils" | ||
languageName: unknown | ||
linkType: soft | ||
|
||
"@segment/analytics-next@workspace:*, @segment/analytics-next@workspace:packages/browser": | ||
version: 0.0.0-use.local | ||
resolution: "@segment/analytics-next@workspace:packages/browser" | ||
|
@@ -3811,6 +3817,7 @@ __metadata: | |
"@internal/config": 0.0.0 | ||
"@lukeed/uuid": ^2.0.0 | ||
"@segment/analytics-core": 1.3.2 | ||
"@segment/analytics-generic-utils": 0.0.1 | ||
"@types/node": ^16 | ||
axios: ^1.4.0 | ||
buffer: ^6.0.3 | ||
|