Skip to main content

Property

Property provides static helper methods that implement the combined getter/setter pattern used across all Oidis model, args, and entity classes.

Package: io-oidis-commons Import: @io-oidis-commons/Io/Oidis/Commons/Utils/Property.js


The Property Pattern​

Every property in an Oidis class follows the same convention:

public MyValue($value? : string) : string {
return this.myValue = Property.String(this.myValue, $value);
}
  • Get — call without argument: obj.MyValue() → returns current value
  • Set — call with argument: obj.MyValue("hello") → sets and returns new value

This pattern is used in ProgramArgs, @Entity models, configuration structures, and anywhere a class exposes typed properties.

No need for manual type conversion

Property.Integer, Property.Boolean, etc. automatically handle string-to-type coercion. Never wrap the argument in parseInt(), parseFloat(), or Boolean() before passing it — Property already does this internally.

// WRONG — redundant manual conversion
case "--count":
this.Count(parseInt($value, 10) || 0);
break;

// CORRECT — Property.Integer converts strings automatically
case "--count":
this.Count($value);
break;

Methods​

Property.String($property, $value?)​

String getter/setter. Empty strings and null are treated as not set — the property keeps its current value. Never returns null; defaults to "".

public Name($value? : string) : string {
return this.name = Property.String(this.name, $value);
}

obj.Name("Alice"); // sets → "Alice"
obj.Name(); // gets → "Alice"
obj.Name(""); // ignored (empty = not set) → "Alice"

Property.NullString($property, $value?)​

Like String, but null is a valid value to set explicitly.

public Comment($value? : string) : string {
return this.comment = Property.NullString(this.comment, $value);
}

obj.Comment(null); // explicitly sets null
obj.Comment(); // gets → null

Property.Integer($property, $value?, $min?, $max?)​

Integer getter/setter. Accepts string | number:

  • Strings are converted via StringUtils.ToInteger (handles "42" → 42)
  • Floats are floored via Math.floor
  • NaN or invalid strings leave the property unchanged
  • Optional $min / $max clamp the accepted range
public Count($value? : string | number) : number {
return this.count = Property.Integer(this.count, $value);
}

obj.Count("10"); // → 10
obj.Count(3.9); // → 3 (floored)
obj.Count("abc"); // → unchanged (NaN ignored)
// With range constraint (only accepts 1–100)
Property.Integer(this.limit, $value, 1, 100);

Property.PositiveInteger($property, $value?, $min?, $max?)​

Like Integer, but only accepts values >= 0. Negative numbers are ignored.


Property.Boolean($property, $value?)​

Boolean getter/setter. Accepts boolean | number | string:

  • true / false → direct
  • 1 / 0 (integer) → true / false
  • "true" / "false" (string) → converted via StringUtils.ToBoolean
public IsEnabled($value? : boolean) : boolean {
return this.isEnabled = Property.Boolean(this.isEnabled, $value);
}

obj.IsEnabled(true); // → true
obj.IsEnabled("false"); // → false
obj.IsEnabled(1); // → true

Property.EnumType($property, $value?, $propertyClassName?, $defaultValue?)​

Enum getter/setter. Validates the value is a member of the given enum class before setting.

public Status($value? : string) : string {
return this.status = Property.EnumType(this.status, $value, TaskStatus, TaskStatus.PENDING);
}

obj.Status("invalid"); // ignored — not a TaskStatus member
obj.Status("COMPLETED"); // → "COMPLETED"

Property.Time($property, $value?)​

Timestamp getter/setter. Accepts:

  • ISO date strings: "2026-03-07T12:00:00Z"
  • Relative time strings: "2h", "30min", "1d", "2w", "3m", "1y"
  • Relative with sign: "+1d" (future), "-2h" (past from now)
  • Raw numeric timestamps
obj.ExpiresAt("+30min");   // 30 minutes from now (as timestamp)
obj.ExpiresAt("2026-12-31T00:00:00Z");

Property.Size($property, $value?)​

Integer getter/setter for byte sizes. Accepts numeric or string with unit suffix:

SuffixMultiplier
B / b× 1
K / k× 1 024
M× 1 048 576
G× 1 073 741 824
T× 1 099 511 627 776
obj.MaxSize("10M");    // → 10 485 760
obj.MaxSize("2G"); // → 2 147 483 648
obj.MaxSize(1024); // → 1 024

Full Example​

import { Property } from "@io-oidis-commons/Io/Oidis/Commons/Utils/Property.js";
import { ProgramArgs as Parent } from "@io-oidis-localhost/Io/Oidis/Localhost/Structures/ProgramArgs.js";

export class ProgramArgs extends Parent {
private ticketKey : string;
private isDryRun : boolean;
private maxRetries : number;

constructor() {
super();
this.ticketKey = "";
this.isDryRun = false;
this.maxRetries = 3;
}

public TicketKey($value? : string) : string {
return this.ticketKey = Property.String(this.ticketKey, $value);
}

public IsDryRun($value? : boolean) : boolean {
return this.isDryRun = Property.Boolean(this.isDryRun, $value);
}

public MaxRetries($value? : string | number) : number {
return this.maxRetries = Property.Integer(this.maxRetries, $value, 1, 10);
}

public processArg($key : string, $value : string) : boolean {
switch ($key) {
case "--ticket":
this.TicketKey($value);
break;
case "--dry-run":
this.IsDryRun(true);
break;
case "--max-retries":
this.MaxRetries($value); // string "5" converted automatically
break;
default:
return super.processArg($key, $value);
}
return true;
}
}

See also: ObjectValidator | StringUtils | Naming Conventions