@kakasoo/proto-typescript
Advanced tools
Comparing version 1.26.2 to 1.26.4
@@ -11,2 +11,3 @@ import { ToPrimitive } from '../interfaces'; | ||
* const yn = TypedBoolean.refine({ true: 'Y', false: 'N' }); | ||
* | ||
* const t = yn('Y'); // true | ||
@@ -17,5 +18,9 @@ * const f = yn('N'); // false | ||
* ``` | ||
* @param map | ||
* @param map Defines the key name that you want to specify with true and false. | ||
* @returns | ||
*/ | ||
refine<T extends string, F extends string>(map: { | ||
true: T; | ||
false: F; | ||
}): <Choice extends boolean | T | F>(data: boolean | Choice) => Equal<Choice, T> extends true ? true : Equal<Choice, F> extends true ? false : Equal<Choice, true> extends true ? T : Equal<Choice, false> extends true ? F : never; | ||
static refine<T extends string, F extends string>(map: { | ||
@@ -22,0 +27,0 @@ true: T; |
@@ -15,2 +15,3 @@ "use strict"; | ||
* const yn = TypedBoolean.refine({ true: 'Y', false: 'N' }); | ||
* | ||
* const t = yn('Y'); // true | ||
@@ -21,5 +22,21 @@ * const f = yn('N'); // false | ||
* ``` | ||
* @param map | ||
* @param map Defines the key name that you want to specify with true and false. | ||
* @returns | ||
*/ | ||
refine(map) { | ||
return function (data) { | ||
if (data === map.true) { | ||
return true; | ||
} | ||
else if (data === map.false) { | ||
return false; | ||
} | ||
else if (data === true) { | ||
return map.true; | ||
} | ||
else { | ||
return map.false; | ||
} | ||
}; | ||
} | ||
static refine(map) { | ||
@@ -26,0 +43,0 @@ return function (data) { |
@@ -60,7 +60,7 @@ import { ToPrimitive } from '../interfaces/to-primitive.interface'; | ||
*/ | ||
padStart<TargetLength extends number = StringType.Length<`${T}`>, PadString extends string = ' '>(targetLength: TargetLength, padString: PadString): TypedString<ReturnType<typeof StringPrototype.padStart<`${T}`, TargetLength, PadString>>>; | ||
padStart<TargetLength extends number = StringType.Length<`${T}`>, PadString extends string = ' '>(targetLength?: TargetLength | TypedNumber<TargetLength>, padString?: PadString | TypedString<PadString>): TypedString<ReturnType<typeof StringPrototype.padStart<`${T}`, TargetLength, PadString>>>; | ||
/** | ||
* @inheritdoc | ||
*/ | ||
padEnd<TargetLength extends number, PadString extends string>(targetLength: TargetLength, padString: PadString): TypedString<ReturnType<typeof StringPrototype.padEnd<`${T}`, TargetLength, PadString>>>; | ||
padEnd<TargetLength extends number = StringType.Length<`${T}`>, PadString extends string = ' '>(targetLength?: TargetLength | TypedNumber<TargetLength>, padString?: PadString | TypedString<PadString>): TypedString<ReturnType<typeof StringPrototype.padEnd<`${T}`, TargetLength, PadString>>>; | ||
/** | ||
@@ -84,3 +84,3 @@ * @inheritdoc | ||
*/ | ||
at<Index extends number, RETURN_TYPE extends NumberType.Compare<Index, '<', StringType.Length<`${T}`>> extends true ? TypedString<ReturnType<typeof StringPrototype.at<`${T}`, Index>>> : undefined>(index?: Index | TypedNumber<Index>): RETURN_TYPE; | ||
at<Index extends number = 0>(index?: Index | TypedNumber<Index>): (Index extends StringType.Length<`${T}`> ? false : [NumberType.Sub<StringType.Length<`${T}`>, Index>] extends [never] ? false : true) extends true ? TypedString<StringType.At<`${T}`, Index>> : undefined; | ||
/** | ||
@@ -87,0 +87,0 @@ * type-safe split. |
@@ -127,4 +127,6 @@ "use strict"; | ||
*/ | ||
padStart(targetLength, padString) { | ||
const initialValue = prototypes_1.StringPrototype.padStart(this.string, targetLength, padString ?? ' '); | ||
padStart(targetLength = new typed_number_class_1.TypedNumber(this.length), padString = new TypedString(' ')) { | ||
const primitiveTargetLength = this.isTypedClass(targetLength) ? targetLength.toPrimitive() : targetLength; | ||
const primitivePadString = this.isTypedClass(padString) ? padString.toPrimitive() : padString; | ||
const initialValue = prototypes_1.StringPrototype.padStart(this.string, primitiveTargetLength, primitivePadString); | ||
return new TypedString(initialValue); | ||
@@ -135,4 +137,6 @@ } | ||
*/ | ||
padEnd(targetLength, padString) { | ||
const initialValue = prototypes_1.StringPrototype.padEnd(this.string, targetLength, padString ?? ' '); | ||
padEnd(targetLength = new typed_number_class_1.TypedNumber(this.length), padString = new TypedString(' ')) { | ||
const primitiveTargetLength = this.isTypedClass(targetLength) ? targetLength.toPrimitive() : targetLength; | ||
const primitivePadString = this.isTypedClass(padString) ? padString.toPrimitive() : padString; | ||
const initialValue = prototypes_1.StringPrototype.padEnd(this.string, primitiveTargetLength, primitivePadString); | ||
return new TypedString(initialValue); | ||
@@ -139,0 +143,0 @@ } |
@@ -0,3 +1,5 @@ | ||
export * from './index-signature.interface'; | ||
export * from './is-typed.interface'; | ||
export * from './to-primitive.interface'; | ||
export * from './to-typed-number.interface'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -17,4 +17,6 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./index-signature.interface"), exports); | ||
__exportStar(require("./is-typed.interface"), exports); | ||
__exportStar(require("./to-primitive.interface"), exports); | ||
__exportStar(require("./to-typed-number.interface"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -77,2 +77,5 @@ import { ArrayType } from './array.type'; | ||
export type _Round<N extends number, TargetDigit extends number> = N extends StringType.IsDecimal<N, number, number> ? StringType.InsertedFractional<N> extends string ? StringType.At<StringType.InsertedFractional<N>, TargetDigit> extends '5' | '6' | '7' | '8' | '9' ? `${StringType.InsertedInteger<N>}.${CeilFractional<StringType.InsertedFractional<N>, TargetDigit>}` : `${StringType.InsertedInteger<N>}.${StringType.ThrowRight<StringType.InsertedFractional<N>, NumberType.Sub<TargetDigit, 2>>}` : N : N; | ||
/** | ||
* @todo Implement. Currently, It's not enough. | ||
*/ | ||
export type Round<N extends number, TargetDigit extends number> = _Round<StringType.ToNumber<StringType.Take<`${N}`, NumberType.Add<TargetDigit, 3>>>, TargetDigit>; | ||
@@ -79,0 +82,0 @@ export {}; |
{ | ||
"name": "@kakasoo/proto-typescript", | ||
"version": "1.26.2", | ||
"version": "1.26.4", | ||
"publishConfig": { | ||
@@ -24,3 +24,5 @@ "access": "public" | ||
"prototype", | ||
"utility-types" | ||
"utility-types", | ||
"iterable/iterator", | ||
"ES6" | ||
], | ||
@@ -27,0 +29,0 @@ "author": "kakasoo", |
@@ -6,3 +6,49 @@ # proto-typescript | ||
# TypedClass | ||
You can use the same method as the prototype of JavaScript. | ||
## prototype methods | ||
```ts | ||
new TypedArray([1, 2, 3] as const).unshift(4 as const); // TypedArray<[4, 1, 2, 3]> | ||
``` | ||
```ts | ||
new TypedArray([1, 2, 3] as const).join(','); // TypedString<'1,2,3'> | ||
``` | ||
The return result of 'TypeClass' returns one of the instances of TypeClass again. Therefore, those who know the grammar of JavaScript can use it immediately. | ||
## `refined` methods | ||
```ts | ||
/** | ||
* error case | ||
*/ | ||
new TypedArray<'1-3'>([1, 2, 3]).refine([1, 2, 3] as const).join('-'); | ||
new TypedArray<'1-3'>([]).refine([1, 2, 3] as const).join('-'); | ||
new TypedArray<'1-3'>().refine([1, 2, 3, 4] as const).join('-'); | ||
new TypedArray<'1-3'>().refine([0, 1, 2, 3] as const).join('-'); | ||
/** | ||
* right case | ||
*/ | ||
new TypedArray<'1-3'>().refine([2, 3] as const).join('-'); | ||
``` | ||
`TypedArray`, `TypedNumber` has the `refine` method, which serves to further narrow down the generic type received as a factor. This helps reduce developer mistakes, but it should be careful because it cannot guarantee actual runtime behavior. | ||
> In the future, improvements will be made at the type level. | ||
## toPrimitive method | ||
```ts | ||
new TypedString('abcdef').toPrimitive(); // 'abcdef' | ||
``` | ||
You can change it to the primitive type of JavaScript. | ||
```ts | ||
const arr = [...new TypedArray([1, 2, 3, 4, 5] as const)]; // (1 | 2 | 3 | 4 | 5)[] | ||
``` | ||
`Iterable/Iterator` is supported. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
193034
1889
54