@philiprehberger/ts-feature-flag 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +44 -0
- package/dist/index.cjs +33 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +14 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +31 -0
- package/dist/index.js.map +1 -0
- package/package.json +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 philiprehberger
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# @philiprehberger/feature-flag
|
|
2
|
+
|
|
3
|
+
Simple in-memory feature flag system with percentage rollouts and targeting.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @philiprehberger/feature-flag
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { createFlags } from '@philiprehberger/feature-flag';
|
|
15
|
+
|
|
16
|
+
const flags = createFlags({
|
|
17
|
+
newCheckout: true,
|
|
18
|
+
darkMode: { percentage: 50 },
|
|
19
|
+
betaFeature: { enabled: (ctx) => ctx.role === 'beta' },
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
flags.enabled('newCheckout'); // true
|
|
23
|
+
flags.enabled('darkMode', { userId: 'abc' }); // deterministic by userId
|
|
24
|
+
flags.enabled('betaFeature', { role: 'beta' }); // true
|
|
25
|
+
flags.enabled('betaFeature', { role: 'user' }); // false
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## API
|
|
29
|
+
|
|
30
|
+
| Export | Description |
|
|
31
|
+
|--------|-------------|
|
|
32
|
+
| `createFlags(definitions)` | Create a flags instance |
|
|
33
|
+
|
|
34
|
+
### Flag Types
|
|
35
|
+
|
|
36
|
+
| Type | Example | Description |
|
|
37
|
+
|------|---------|-------------|
|
|
38
|
+
| Boolean | `true` / `false` | Static on/off |
|
|
39
|
+
| Percentage | `{ percentage: 50 }` | Deterministic rollout by userId hash |
|
|
40
|
+
| Function | `{ enabled: (ctx) => ... }` | Custom targeting logic |
|
|
41
|
+
|
|
42
|
+
## License
|
|
43
|
+
|
|
44
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/flags.ts
|
|
4
|
+
function hashString(str) {
|
|
5
|
+
let hash = 0;
|
|
6
|
+
for (let i = 0; i < str.length; i++) {
|
|
7
|
+
hash = (hash << 5) - hash + str.charCodeAt(i) | 0;
|
|
8
|
+
}
|
|
9
|
+
return Math.abs(hash);
|
|
10
|
+
}
|
|
11
|
+
function createFlags(definitions) {
|
|
12
|
+
return {
|
|
13
|
+
enabled(name, context) {
|
|
14
|
+
const flag = definitions[name];
|
|
15
|
+
if (flag === void 0) return false;
|
|
16
|
+
if (typeof flag === "boolean") return flag;
|
|
17
|
+
const def = flag;
|
|
18
|
+
if ("percentage" in def) {
|
|
19
|
+
const seed = (context == null ? void 0 : context.userId) ? String(context.userId) : (context == null ? void 0 : context.id) ? String(context.id) : String(Math.random());
|
|
20
|
+
const hash = hashString(`${String(name)}:${seed}`);
|
|
21
|
+
return hash % 100 < def.percentage;
|
|
22
|
+
}
|
|
23
|
+
if ("enabled" in def) {
|
|
24
|
+
return def.enabled(context != null ? context : {});
|
|
25
|
+
}
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
exports.createFlags = createFlags;
|
|
32
|
+
//# sourceMappingURL=index.cjs.map
|
|
33
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/flags.ts"],"names":[],"mappings":";;;AAEA,SAAS,WAAW,GAAA,EAAqB;AACvC,EAAA,IAAI,IAAA,GAAO,CAAA;AACX,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,GAAA,CAAI,QAAQ,CAAA,EAAA,EAAK;AACnC,IAAA,IAAA,GAAA,CAAS,QAAQ,CAAA,IAAK,IAAA,GAAO,GAAA,CAAI,UAAA,CAAW,CAAC,CAAA,GAAK,CAAA;AAAA,EACpD;AACA,EAAA,OAAO,IAAA,CAAK,IAAI,IAAI,CAAA;AACtB;AAEO,SAAS,YAAuC,WAAA,EAA0B;AAC/E,EAAA,OAAO;AAAA,IACL,OAAA,CAAQ,MAAe,OAAA,EAAgC;AACrD,MAAA,MAAM,IAAA,GAAO,YAAY,IAAI,CAAA;AAC7B,MAAA,IAAI,IAAA,KAAS,QAAW,OAAO,KAAA;AAE/B,MAAA,IAAI,OAAO,IAAA,KAAS,SAAA,EAAW,OAAO,IAAA;AAEtC,MAAA,MAAM,GAAA,GAAM,IAAA;AAEZ,MAAA,IAAI,gBAAgB,GAAA,EAAK;AACvB,QAAA,MAAM,QAAO,OAAA,IAAA,IAAA,GAAA,MAAA,GAAA,OAAA,CAAS,MAAA,IAClB,MAAA,CAAO,OAAA,CAAQ,MAAM,CAAA,GAAA,CACrB,OAAA,IAAA,IAAA,GAAA,MAAA,GAAA,OAAA,CAAS,EAAA,IACP,MAAA,CAAO,QAAQ,EAAE,CAAA,GACjB,MAAA,CAAO,IAAA,CAAK,QAAQ,CAAA;AAC1B,QAAA,MAAM,IAAA,GAAO,WAAW,CAAA,EAAG,MAAA,CAAO,IAAI,CAAC,CAAA,CAAA,EAAI,IAAI,CAAA,CAAE,CAAA;AACjD,QAAA,OAAQ,IAAA,GAAO,MAAO,GAAA,CAAI,UAAA;AAAA,MAC5B;AAEA,MAAA,IAAI,aAAa,GAAA,EAAK;AACpB,QAAA,OAAO,GAAA,CAAI,OAAA,CAAQ,OAAA,IAAA,IAAA,GAAA,OAAA,GAAW,EAAE,CAAA;AAAA,MAClC;AAEA,MAAA,OAAO,KAAA;AAAA,IACT;AAAA,GACF;AACF","file":"index.cjs","sourcesContent":["import type { FlagDefinitions, FlagContext, Flags } from './types.js';\n\nfunction hashString(str: string): number {\n let hash = 0;\n for (let i = 0; i < str.length; i++) {\n hash = ((hash << 5) - hash + str.charCodeAt(i)) | 0;\n }\n return Math.abs(hash);\n}\n\nexport function createFlags<T extends FlagDefinitions>(definitions: T): Flags<T> {\n return {\n enabled(name: keyof T, context?: FlagContext): boolean {\n const flag = definitions[name];\n if (flag === undefined) return false;\n\n if (typeof flag === 'boolean') return flag;\n\n const def = flag as { percentage: number } | { enabled: (context: FlagContext) => boolean };\n\n if ('percentage' in def) {\n const seed = context?.userId\n ? String(context.userId)\n : context?.id\n ? String(context.id)\n : String(Math.random());\n const hash = hashString(`${String(name)}:${seed}`);\n return (hash % 100) < def.percentage;\n }\n\n if ('enabled' in def) {\n return def.enabled(context ?? {});\n }\n\n return false;\n },\n };\n}\n"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
type FlagContext = Record<string, unknown>;
|
|
2
|
+
type FlagDefinition = boolean | {
|
|
3
|
+
percentage: number;
|
|
4
|
+
} | {
|
|
5
|
+
enabled: (context: FlagContext) => boolean;
|
|
6
|
+
};
|
|
7
|
+
type FlagDefinitions = Record<string, FlagDefinition>;
|
|
8
|
+
interface Flags<T extends FlagDefinitions> {
|
|
9
|
+
enabled(name: keyof T, context?: FlagContext): boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
declare function createFlags<T extends FlagDefinitions>(definitions: T): Flags<T>;
|
|
13
|
+
|
|
14
|
+
export { type FlagContext, type FlagDefinition, type FlagDefinitions, type Flags, createFlags };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
type FlagContext = Record<string, unknown>;
|
|
2
|
+
type FlagDefinition = boolean | {
|
|
3
|
+
percentage: number;
|
|
4
|
+
} | {
|
|
5
|
+
enabled: (context: FlagContext) => boolean;
|
|
6
|
+
};
|
|
7
|
+
type FlagDefinitions = Record<string, FlagDefinition>;
|
|
8
|
+
interface Flags<T extends FlagDefinitions> {
|
|
9
|
+
enabled(name: keyof T, context?: FlagContext): boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
declare function createFlags<T extends FlagDefinitions>(definitions: T): Flags<T>;
|
|
13
|
+
|
|
14
|
+
export { type FlagContext, type FlagDefinition, type FlagDefinitions, type Flags, createFlags };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// src/flags.ts
|
|
2
|
+
function hashString(str) {
|
|
3
|
+
let hash = 0;
|
|
4
|
+
for (let i = 0; i < str.length; i++) {
|
|
5
|
+
hash = (hash << 5) - hash + str.charCodeAt(i) | 0;
|
|
6
|
+
}
|
|
7
|
+
return Math.abs(hash);
|
|
8
|
+
}
|
|
9
|
+
function createFlags(definitions) {
|
|
10
|
+
return {
|
|
11
|
+
enabled(name, context) {
|
|
12
|
+
const flag = definitions[name];
|
|
13
|
+
if (flag === void 0) return false;
|
|
14
|
+
if (typeof flag === "boolean") return flag;
|
|
15
|
+
const def = flag;
|
|
16
|
+
if ("percentage" in def) {
|
|
17
|
+
const seed = (context == null ? void 0 : context.userId) ? String(context.userId) : (context == null ? void 0 : context.id) ? String(context.id) : String(Math.random());
|
|
18
|
+
const hash = hashString(`${String(name)}:${seed}`);
|
|
19
|
+
return hash % 100 < def.percentage;
|
|
20
|
+
}
|
|
21
|
+
if ("enabled" in def) {
|
|
22
|
+
return def.enabled(context != null ? context : {});
|
|
23
|
+
}
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export { createFlags };
|
|
30
|
+
//# sourceMappingURL=index.js.map
|
|
31
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/flags.ts"],"names":[],"mappings":";AAEA,SAAS,WAAW,GAAA,EAAqB;AACvC,EAAA,IAAI,IAAA,GAAO,CAAA;AACX,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,GAAA,CAAI,QAAQ,CAAA,EAAA,EAAK;AACnC,IAAA,IAAA,GAAA,CAAS,QAAQ,CAAA,IAAK,IAAA,GAAO,GAAA,CAAI,UAAA,CAAW,CAAC,CAAA,GAAK,CAAA;AAAA,EACpD;AACA,EAAA,OAAO,IAAA,CAAK,IAAI,IAAI,CAAA;AACtB;AAEO,SAAS,YAAuC,WAAA,EAA0B;AAC/E,EAAA,OAAO;AAAA,IACL,OAAA,CAAQ,MAAe,OAAA,EAAgC;AACrD,MAAA,MAAM,IAAA,GAAO,YAAY,IAAI,CAAA;AAC7B,MAAA,IAAI,IAAA,KAAS,QAAW,OAAO,KAAA;AAE/B,MAAA,IAAI,OAAO,IAAA,KAAS,SAAA,EAAW,OAAO,IAAA;AAEtC,MAAA,MAAM,GAAA,GAAM,IAAA;AAEZ,MAAA,IAAI,gBAAgB,GAAA,EAAK;AACvB,QAAA,MAAM,QAAO,OAAA,IAAA,IAAA,GAAA,MAAA,GAAA,OAAA,CAAS,MAAA,IAClB,MAAA,CAAO,OAAA,CAAQ,MAAM,CAAA,GAAA,CACrB,OAAA,IAAA,IAAA,GAAA,MAAA,GAAA,OAAA,CAAS,EAAA,IACP,MAAA,CAAO,QAAQ,EAAE,CAAA,GACjB,MAAA,CAAO,IAAA,CAAK,QAAQ,CAAA;AAC1B,QAAA,MAAM,IAAA,GAAO,WAAW,CAAA,EAAG,MAAA,CAAO,IAAI,CAAC,CAAA,CAAA,EAAI,IAAI,CAAA,CAAE,CAAA;AACjD,QAAA,OAAQ,IAAA,GAAO,MAAO,GAAA,CAAI,UAAA;AAAA,MAC5B;AAEA,MAAA,IAAI,aAAa,GAAA,EAAK;AACpB,QAAA,OAAO,GAAA,CAAI,OAAA,CAAQ,OAAA,IAAA,IAAA,GAAA,OAAA,GAAW,EAAE,CAAA;AAAA,MAClC;AAEA,MAAA,OAAO,KAAA;AAAA,IACT;AAAA,GACF;AACF","file":"index.js","sourcesContent":["import type { FlagDefinitions, FlagContext, Flags } from './types.js';\n\nfunction hashString(str: string): number {\n let hash = 0;\n for (let i = 0; i < str.length; i++) {\n hash = ((hash << 5) - hash + str.charCodeAt(i)) | 0;\n }\n return Math.abs(hash);\n}\n\nexport function createFlags<T extends FlagDefinitions>(definitions: T): Flags<T> {\n return {\n enabled(name: keyof T, context?: FlagContext): boolean {\n const flag = definitions[name];\n if (flag === undefined) return false;\n\n if (typeof flag === 'boolean') return flag;\n\n const def = flag as { percentage: number } | { enabled: (context: FlagContext) => boolean };\n\n if ('percentage' in def) {\n const seed = context?.userId\n ? String(context.userId)\n : context?.id\n ? String(context.id)\n : String(Math.random());\n const hash = hashString(`${String(name)}:${seed}`);\n return (hash % 100) < def.percentage;\n }\n\n if ('enabled' in def) {\n return def.enabled(context ?? {});\n }\n\n return false;\n },\n };\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@philiprehberger/ts-feature-flag",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Simple in-memory feature flag system with percentage rollouts and targeting",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./dist/index.d.cts",
|
|
17
|
+
"default": "./dist/index.cjs"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsup",
|
|
26
|
+
"dev": "tsup --watch",
|
|
27
|
+
"typecheck": "tsc --noEmit",
|
|
28
|
+
"prepublishOnly": "npm run build"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"tsup": "^8.0.0",
|
|
32
|
+
"typescript": "^5.0.0"
|
|
33
|
+
},
|
|
34
|
+
"keywords": [
|
|
35
|
+
"feature-flag",
|
|
36
|
+
"toggle",
|
|
37
|
+
"rollout",
|
|
38
|
+
"targeting",
|
|
39
|
+
"feature-toggle"
|
|
40
|
+
],
|
|
41
|
+
"license": "MIT",
|
|
42
|
+
"repository": {
|
|
43
|
+
"type": "git",
|
|
44
|
+
"url": "git+https://github.com/philiprehberger/ts-feature-flag.git"
|
|
45
|
+
},
|
|
46
|
+
"homepage": "https://github.com/philiprehberger/ts-feature-flag#readme",
|
|
47
|
+
"bugs": {
|
|
48
|
+
"url": "https://github.com/philiprehberger/ts-feature-flag/issues"
|
|
49
|
+
},
|
|
50
|
+
"author": "Philip Rehberger",
|
|
51
|
+
"engines": {
|
|
52
|
+
"node": ">=18.0.0"
|
|
53
|
+
},
|
|
54
|
+
"sideEffects": false
|
|
55
|
+
}
|