@splitlab/core 0.1.0
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/dist/index.cjs +1 -0
- package/dist/index.d.cts +66 -0
- package/dist/index.d.ts +66 -0
- package/dist/index.js +1 -0
- package/package.json +36 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var v=Object.defineProperty;var x=Object.getOwnPropertyDescriptor;var C=Object.getOwnPropertyNames;var y=Object.prototype.hasOwnProperty;var S=(n,a)=>{for(var t in a)v(n,t,{get:a[t],enumerable:!0})},R=(n,a,t,o)=>{if(a&&typeof a=="object"||typeof a=="function")for(let r of C(a))!y.call(n,r)&&r!==t&&v(n,r,{get:()=>a[r],enumerable:!(o=x(a,r))||o.enumerable});return n};var T=n=>R(v({},"__esModule",{value:!0}),n);var k={};S(k,{evaluateRules:()=>m,hashToFloat:()=>p,localEvaluate:()=>b,murmurhash3:()=>g});module.exports=T(k);function g(n,a=0){let t=a>>>0,o=n.length,r=o>>2,e=3432918353,l=461845907;for(let s=0;s<r;s++){let i=n.charCodeAt(s*4)&255|(n.charCodeAt(s*4+1)&255)<<8|(n.charCodeAt(s*4+2)&255)<<16|(n.charCodeAt(s*4+3)&255)<<24;i=Math.imul(i,e),i=i<<15|i>>>17,i=Math.imul(i,l),t^=i,t=t<<13|t>>>19,t=Math.imul(t,5)+3864292196}let c=r*4,u=0;switch(o&3){case 3:u^=(n.charCodeAt(c+2)&255)<<16;case 2:u^=(n.charCodeAt(c+1)&255)<<8;case 1:u^=n.charCodeAt(c)&255,u=Math.imul(u,e),u=u<<15|u>>>17,u=Math.imul(u,l),t^=u}return t^=o,t^=t>>>16,t=Math.imul(t,2246822507),t^=t>>>13,t=Math.imul(t,3266489909),t^=t>>>16,t>>>0}function p(n,a){return g(n,a)/4294967295}function b(n,a,t={}){let o={},r={};for(let e of n.experiments){if(e.targeting_rules&&!m(e.targeting_rules,t)){o[e.key]=null;continue}let l=g(e.key+":"+a);if(l%1e4/100>=e.traffic_percentage){o[e.key]=null;continue}let u=e.variants.reduce((h,d)=>h+d.weight,0),s=l%u,i=0,f=null;for(let h of e.variants)if(i+=h.weight,s<i){f=h.key;break}f||(f=e.variants[e.variants.length-1].key),o[e.key]=f}for(let e of n.flags){if(e.rules&&!m(e.rules,t)){r[e.key]=!1;continue}let c=g(e.key+":"+a)%100;r[e.key]=c<e.rollout_percentage}return{experiments:o,flags:r}}function m(n,a){if(!n.groups||n.groups.length===0)return!0;let t=r=>{let e=a[r.attribute];if(e==null)return!1;switch(r.operator){case"is":return String(e)===String(r.value);case"is_not":return String(e)!==String(r.value);case"contains":return String(e).toLowerCase().includes(String(r.value).toLowerCase());case"not_contains":return!String(e).toLowerCase().includes(String(r.value).toLowerCase());case"gt":return Number(e)>Number(r.value);case"lt":return Number(e)<Number(r.value);case"gte":return Number(e)>=Number(r.value);case"lte":return Number(e)<=Number(r.value);case"in":return Array.isArray(r.value)&&r.value.map(String).includes(String(e));case"not_in":return Array.isArray(r.value)&&!r.value.map(String).includes(String(e));default:return!1}},o=r=>r.conditions.every(t);return n.match==="any"?n.groups.some(o):n.groups.every(o)}0&&(module.exports={evaluateRules,hashToFloat,localEvaluate,murmurhash3});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MurmurHash3_x86_32 — pure TypeScript implementation.
|
|
3
|
+
* Identical to api/src/utils/hash.ts — the SDK and server
|
|
4
|
+
* MUST produce the same output for local evaluation to work.
|
|
5
|
+
*/
|
|
6
|
+
declare function murmurhash3(key: string, seed?: number): number;
|
|
7
|
+
/**
|
|
8
|
+
* Hash a key to a float in [0, 1).
|
|
9
|
+
*/
|
|
10
|
+
declare function hashToFloat(key: string, seed?: number): number;
|
|
11
|
+
|
|
12
|
+
interface Variant {
|
|
13
|
+
key: string;
|
|
14
|
+
weight: number;
|
|
15
|
+
}
|
|
16
|
+
interface TargetingCondition {
|
|
17
|
+
attribute: string;
|
|
18
|
+
operator: 'is' | 'is_not' | 'contains' | 'not_contains' | 'gt' | 'lt' | 'gte' | 'lte' | 'in' | 'not_in';
|
|
19
|
+
value: string | number | string[];
|
|
20
|
+
}
|
|
21
|
+
interface TargetingGroup {
|
|
22
|
+
conditions: TargetingCondition[];
|
|
23
|
+
}
|
|
24
|
+
interface TargetingRules {
|
|
25
|
+
match: 'all' | 'any';
|
|
26
|
+
groups: TargetingGroup[];
|
|
27
|
+
}
|
|
28
|
+
interface ExperimentConfig {
|
|
29
|
+
key: string;
|
|
30
|
+
traffic_percentage: number;
|
|
31
|
+
variants: Variant[];
|
|
32
|
+
targeting_rules?: TargetingRules | null;
|
|
33
|
+
}
|
|
34
|
+
interface FlagConfig {
|
|
35
|
+
key: string;
|
|
36
|
+
enabled: boolean;
|
|
37
|
+
rollout_percentage: number;
|
|
38
|
+
rules?: TargetingRules | null;
|
|
39
|
+
}
|
|
40
|
+
interface ServerConfig {
|
|
41
|
+
experiments: ExperimentConfig[];
|
|
42
|
+
flags: FlagConfig[];
|
|
43
|
+
}
|
|
44
|
+
interface EvalResult {
|
|
45
|
+
experiments: Record<string, string | null>;
|
|
46
|
+
flags: Record<string, boolean>;
|
|
47
|
+
}
|
|
48
|
+
interface TrackEvent {
|
|
49
|
+
distinct_id: string;
|
|
50
|
+
event_name: string;
|
|
51
|
+
properties?: Record<string, any>;
|
|
52
|
+
timestamp?: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Locally evaluate all experiments and flags for a given distinctId.
|
|
57
|
+
* Pure function — no side effects or external state.
|
|
58
|
+
*/
|
|
59
|
+
declare function localEvaluate(config: ServerConfig, distinctId: string, attributes?: Record<string, any>): EvalResult;
|
|
60
|
+
/**
|
|
61
|
+
* Evaluate targeting rules against a set of attributes.
|
|
62
|
+
* Returns true if the rules match (user is targeted).
|
|
63
|
+
*/
|
|
64
|
+
declare function evaluateRules(rules: TargetingRules, attributes: Record<string, any>): boolean;
|
|
65
|
+
|
|
66
|
+
export { type EvalResult, type ExperimentConfig, type FlagConfig, type ServerConfig, type TargetingCondition, type TargetingGroup, type TargetingRules, type TrackEvent, type Variant, evaluateRules, hashToFloat, localEvaluate, murmurhash3 };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MurmurHash3_x86_32 — pure TypeScript implementation.
|
|
3
|
+
* Identical to api/src/utils/hash.ts — the SDK and server
|
|
4
|
+
* MUST produce the same output for local evaluation to work.
|
|
5
|
+
*/
|
|
6
|
+
declare function murmurhash3(key: string, seed?: number): number;
|
|
7
|
+
/**
|
|
8
|
+
* Hash a key to a float in [0, 1).
|
|
9
|
+
*/
|
|
10
|
+
declare function hashToFloat(key: string, seed?: number): number;
|
|
11
|
+
|
|
12
|
+
interface Variant {
|
|
13
|
+
key: string;
|
|
14
|
+
weight: number;
|
|
15
|
+
}
|
|
16
|
+
interface TargetingCondition {
|
|
17
|
+
attribute: string;
|
|
18
|
+
operator: 'is' | 'is_not' | 'contains' | 'not_contains' | 'gt' | 'lt' | 'gte' | 'lte' | 'in' | 'not_in';
|
|
19
|
+
value: string | number | string[];
|
|
20
|
+
}
|
|
21
|
+
interface TargetingGroup {
|
|
22
|
+
conditions: TargetingCondition[];
|
|
23
|
+
}
|
|
24
|
+
interface TargetingRules {
|
|
25
|
+
match: 'all' | 'any';
|
|
26
|
+
groups: TargetingGroup[];
|
|
27
|
+
}
|
|
28
|
+
interface ExperimentConfig {
|
|
29
|
+
key: string;
|
|
30
|
+
traffic_percentage: number;
|
|
31
|
+
variants: Variant[];
|
|
32
|
+
targeting_rules?: TargetingRules | null;
|
|
33
|
+
}
|
|
34
|
+
interface FlagConfig {
|
|
35
|
+
key: string;
|
|
36
|
+
enabled: boolean;
|
|
37
|
+
rollout_percentage: number;
|
|
38
|
+
rules?: TargetingRules | null;
|
|
39
|
+
}
|
|
40
|
+
interface ServerConfig {
|
|
41
|
+
experiments: ExperimentConfig[];
|
|
42
|
+
flags: FlagConfig[];
|
|
43
|
+
}
|
|
44
|
+
interface EvalResult {
|
|
45
|
+
experiments: Record<string, string | null>;
|
|
46
|
+
flags: Record<string, boolean>;
|
|
47
|
+
}
|
|
48
|
+
interface TrackEvent {
|
|
49
|
+
distinct_id: string;
|
|
50
|
+
event_name: string;
|
|
51
|
+
properties?: Record<string, any>;
|
|
52
|
+
timestamp?: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Locally evaluate all experiments and flags for a given distinctId.
|
|
57
|
+
* Pure function — no side effects or external state.
|
|
58
|
+
*/
|
|
59
|
+
declare function localEvaluate(config: ServerConfig, distinctId: string, attributes?: Record<string, any>): EvalResult;
|
|
60
|
+
/**
|
|
61
|
+
* Evaluate targeting rules against a set of attributes.
|
|
62
|
+
* Returns true if the rules match (user is targeted).
|
|
63
|
+
*/
|
|
64
|
+
declare function evaluateRules(rules: TargetingRules, attributes: Record<string, any>): boolean;
|
|
65
|
+
|
|
66
|
+
export { type EvalResult, type ExperimentConfig, type FlagConfig, type ServerConfig, type TargetingCondition, type TargetingGroup, type TargetingRules, type TrackEvent, type Variant, evaluateRules, hashToFloat, localEvaluate, murmurhash3 };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function g(n,i=0){let t=i>>>0,o=n.length,r=o>>2,e=3432918353,l=461845907;for(let s=0;s<r;s++){let u=n.charCodeAt(s*4)&255|(n.charCodeAt(s*4+1)&255)<<8|(n.charCodeAt(s*4+2)&255)<<16|(n.charCodeAt(s*4+3)&255)<<24;u=Math.imul(u,e),u=u<<15|u>>>17,u=Math.imul(u,l),t^=u,t=t<<13|t>>>19,t=Math.imul(t,5)+3864292196}let c=r*4,a=0;switch(o&3){case 3:a^=(n.charCodeAt(c+2)&255)<<16;case 2:a^=(n.charCodeAt(c+1)&255)<<8;case 1:a^=n.charCodeAt(c)&255,a=Math.imul(a,e),a=a<<15|a>>>17,a=Math.imul(a,l),t^=a}return t^=o,t^=t>>>16,t=Math.imul(t,2246822507),t^=t>>>13,t=Math.imul(t,3266489909),t^=t>>>16,t>>>0}function p(n,i){return g(n,i)/4294967295}function b(n,i,t={}){let o={},r={};for(let e of n.experiments){if(e.targeting_rules&&!m(e.targeting_rules,t)){o[e.key]=null;continue}let l=g(e.key+":"+i);if(l%1e4/100>=e.traffic_percentage){o[e.key]=null;continue}let a=e.variants.reduce((h,v)=>h+v.weight,0),s=l%a,u=0,f=null;for(let h of e.variants)if(u+=h.weight,s<u){f=h.key;break}f||(f=e.variants[e.variants.length-1].key),o[e.key]=f}for(let e of n.flags){if(e.rules&&!m(e.rules,t)){r[e.key]=!1;continue}let c=g(e.key+":"+i)%100;r[e.key]=c<e.rollout_percentage}return{experiments:o,flags:r}}function m(n,i){if(!n.groups||n.groups.length===0)return!0;let t=r=>{let e=i[r.attribute];if(e==null)return!1;switch(r.operator){case"is":return String(e)===String(r.value);case"is_not":return String(e)!==String(r.value);case"contains":return String(e).toLowerCase().includes(String(r.value).toLowerCase());case"not_contains":return!String(e).toLowerCase().includes(String(r.value).toLowerCase());case"gt":return Number(e)>Number(r.value);case"lt":return Number(e)<Number(r.value);case"gte":return Number(e)>=Number(r.value);case"lte":return Number(e)<=Number(r.value);case"in":return Array.isArray(r.value)&&r.value.map(String).includes(String(e));case"not_in":return Array.isArray(r.value)&&!r.value.map(String).includes(String(e));default:return!1}},o=r=>r.conditions.every(t);return n.match==="any"?n.groups.some(o):n.groups.every(o)}export{m as evaluateRules,p as hashToFloat,b as localEvaluate,g as murmurhash3};
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@splitlab/core",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Shared evaluation engine for SplitLab SDKs — hashing, targeting, bucketing",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"main": "./dist/index.cjs",
|
|
11
|
+
"module": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"import": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"default": "./dist/index.js"
|
|
18
|
+
},
|
|
19
|
+
"require": {
|
|
20
|
+
"types": "./dist/index.d.cts",
|
|
21
|
+
"default": "./dist/index.cjs"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist"
|
|
27
|
+
],
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "tsup",
|
|
30
|
+
"dev": "tsup --watch"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"tsup": "^8.0.0",
|
|
34
|
+
"typescript": "^5.4.0"
|
|
35
|
+
}
|
|
36
|
+
}
|