Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for import es6 #205

Open
born2net opened this issue Nov 17, 2016 · 3 comments
Open

Support for import es6 #205

born2net opened this issue Nov 17, 2016 · 3 comments

Comments

@born2net
Copy link

Using angular2 aand TypeScript trying to do:

import * as S from 'string';

will not work.
this will work but its a hack

var S = S['default'] as StringJS;

and even with the above TypeScript is not happy with InteliSence

regards

Sean

@born2net
Copy link
Author

born2net commented Nov 17, 2016

ok this is the solution:

declare module "string" {
    var S: {
        (o: any): StringJS;
        default: {
            (o: any): StringJS
        };
        VERSION: string;
        TMPL_OPEN: string;
        TMPL_CLOSE: string;
    }
    export = S;
}

declare var S:{(o: any): StringJS};

and to import you do:

import * as S from 'string';
window['StringJS'] = S.default;

and to use:

var str = StringJS('  String   \t libraries are   \n\n\t fun\n!  ').collapseWhitespace().s;
console.log(str);

but it would be better if the lib exported the default so no need for an extra step.

here is the entire .d.ts file:

// Type definitions for string.js
// Project: http://stringjs.com
// Definitions by: Bas Pennings <https://github.com/basp/>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped

interface StringJS {
    length: number;

    s: string;

    between(left: string, right?: string): StringJS;

    camelize(): StringJS;

    capitalize(): StringJS;

    chompLeft(prefix: string): StringJS;

    chompRight(suffix: string): StringJS;

    collapseWhitespace(): StringJS;

    contains(ss: string): boolean;

    count(substring: string): number;

    dasherize(): StringJS;

    decodeHTMLEntities(): StringJS;

    endsWith(ss: string): boolean;

    escapeHTML(): StringJS;

    ensureLeft(prefix: string): StringJS;

    ensureRight(suffix: string): StringJS;

    humanize(): StringJS;

    include(ss: string): boolean;

    isAlpha(): boolean;

    isAlphaNumeric(): boolean;

    isEmpty(): boolean;

    isLower(): boolean;

    isNumeric(): boolean;

    isUpper(): boolean;

    latinise(): StringJS;

    left(n: number): StringJS;

    lines(): string[];

    pad(len: number, char?: string|number): StringJS;

    padLeft(len: number, char?: string|number): StringJS;

    padRight(len: number, char?: string|number): StringJS;

    parseCSV(delimiter?: string, qualifier?: string, escape?: string, lineDelimiter?: string): string[];

    repeat(n: number): StringJS;

    replaceAll(ss: string, newStr: string): StringJS;

    strip(...strings: string[]): StringJS;

    right(n: number): StringJS;

    setValue(string: any): StringJS;

    slugify(): StringJS;

    startsWith(prefix: string): boolean;

    stripPunctuation(): StringJS;

    stripTags(...tags: string[]): StringJS;

    template(values: Object, open?: string, close?: string): StringJS;

    times(n: number): StringJS;

    toBoolean(): boolean;

    toCSV(delimiter?: string, qualifier?: string): StringJS;
    toCSV(options: {
        delimiter?: string,
        qualifier?: string,
        escape?: string,
        encloseNumbers?: boolean,
        keys?: boolean
    }): StringJS;

    toFloat(precision?: number): number;

    toInt(): number;

    toInteger(): number;

    toString(): string;

    trim(): StringJS;

    trimLeft(): StringJS;

    trimRight(): StringJS;

    truncate(length: number, chars?: string): StringJS;

    underscore(): StringJS;

    unescapeHTML(): StringJS;

    wrapHTML(element?: string, attributes?: Object): StringJS;
}

declare module "string" {
    var S: {
        (o: any): StringJS;
        default: {
            (o: any): StringJS
        };
        VERSION: string;
        TMPL_OPEN: string;
        TMPL_CLOSE: string;
    }
    export = S;
}

declare var S:{(o: any): StringJS};

Angular 2 Kitchen sink: http://ng2.javascriptninja.io
and source@ https://github.com/born2net/Angular-kitchen-sink
Regards,

Sean

@dwilt
Copy link

dwilt commented Dec 15, 2016

I'd like to use this in my react native project. Why can't I do something like this?

import {
    capitalize
} from 'string';

I get:

image

@Frexuz
Copy link

Frexuz commented Nov 27, 2017

Yep, also want import { humanize } from 'string' :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants