jprichardson / string.js

Extra JavaScript string methods.
http://stringjs.com
1.81k stars 234 forks source link

Support for import es6 #205

Open born2net opened 7 years ago

born2net commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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 commented 6 years ago

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