angular-package
TwitterGitHub
Wrapper
Wrapper
  • Introduction
  • ❤ Benefits
    • Explanation
    • Designing
  • General concepts
  • Getting started
    • Skeleton
    • Installation
      • npm
    • Public API
    • Basic concepts
  • Wrap {}
    • Overview
    • Generic type variables
    • ★ Constructor
    • Accessors
      • closing
      • opening
      • text
      • [Symbol.toStringTag]
    • Properties
      • #closing
      • #opening
      • #text
    • Methods
      • Static
        • hasClosing()
        • hasOpening()
        • isWrap()
      • Instance
        • getClosing()
        • getOpening()
        • getText()
        • hasClosing()
        • hasOpening()
        • hasText()
        • isWrapped()
        • ★ replaceClosing()
        • ★ replaceOpening()
        • ★ replaceText()
        • toString()
        • valueOf()
    • Example usage
  • Wrapper {}
    • Overview
    • Generic type variables
    • Constructor
    • Accessors
      • [Symbol.toStringTag]
    • Methods
      • Static
        • define()
        • isWrapper()
        • replaceClosing()
        • replaceOpening()
        • unwrap()
      • Instance
        • isClosingIn()
        • isOpeningIn()
        • replaceClosingIn()
        • replaceOpeningIn()
        • removeWrapIn()
        • textReplaceClosing()
        • textReplaceOpening()
        • ⚠ textHasClosing()
        • ⚠ textHasOpening()
        • textUnwrap()
        • textWrap()
        • toArray()
        • toWrap()
        • unwrap()
        • unwrapText()
        • ★ wrap()
        • ★ wrapOn()
        • ★ wrapText()
    • Example usage
  • Type
    • Wrapped
  • Change log
    • Keep a changelog
    • CHANGELOG.md
    • v1.0.0
  • GIT
    • Commit
    • Semantic Versioning
  • License
    • MIT
  • Contact
    • ⋯ Chat
    • @ Email
  • Donate
    • ฿ Cryptocurrency
    • $ Fiat
  • More versions
Powered by GitBook
On this page
  • Partial primitive value with the exact return type
  • Object as types

Was this helpful?

  1. ❤ Benefits

Explanation

Previous❤ BenefitsNextGeneral concepts

Last updated 3 years ago

Was this helpful?

Partial primitive value with the exact return type

This example explains what partial primitive value and the exact return type means in the string object.

// Example usage.
import { Wrap } from '@angular-package/text';

// Define tag [quote].
const quoteTag = new Wrap(
  // quoteTag.opening;
  `[`,
  // quoteTag.closing;
  `]`,
  // quoteTag.content;
  'quote'
);

// The opening is of a generic type variable Opening in this case it's [.
quoteTag.opening;

// The closing is of a generic type variable Closing in this case it's ].
quoteTag.closing;

// The content is of a generic type variable Content in this case it's quote.
quoteTag.content;

Immutability


// Cannot assign to 'opening' because it is a read-only property
// and the opening is of a generic type variable Opening in this case it's `[`.
quoteTag.opening = ; 

Object as types

There is possibility to check whether the object is the wrapper with the help of typeOf() function. The name of bbCodeWrapper object cannot be changed in the example below.

// Example usage.
import { Wrapper } from '@angular-package/text';
import { typeOf } from '@angular-package/type';

class CustomWrap {
  bbCode: Wrapper<`[`, `]`> = new Wrapper(`[`, `]`);
  html: Wrapper<'<', '>'> = new Wrapper(`<`, `>`);
}

// Returns [quote] of type "[quote]".
new CustomWrap().bbCode.wrap('quote');

// Returns <span> of type "<span>".
new CustomWrap().html.wrap('span');

// Checks the object type. Returns 'wrapper'.
const bbCodeWrapper = new Wrapper('[', ']');

// Returns 'wrapper'.
typeOf(bbCodeWrapper);

// Symbol.toStringTag is immutable. Can't be changed.
// Uncaught TypeError: Cannot set property Symbol(Symbol.toStringTag) of [object Object] which has only a getter
Object.assign(bbCodeWrapper, {
  [Symbol.toStringTag]: 'custom'
});

To create primitive value of object there is the need to provide three parameters in order opening, closing, and optional content. They are stored in separate private properties which are accessible with the help of accessors, and because of this, access to parts of the primitive value is achieved. More, each parameter is of a generic type variable that detects the type of given value, and because of this, the exact return type of primitive value and its parts is achieved.

All the parts that make up the primitive value of the object are immutable because they are accessible only by the get accessors. There is no possibility to change any part of the primitive value of the quoteTag object and any try to change e.g. opening results in the error message "Cannot assign to 'opening' because it is a read-only property".

Wrap
Wrap
Wrap