Giter VIP home page Giter VIP logo

ng2-truncate's Introduction

npm

@yellowspot/ng-truncate

ng-truncate

GitHub issues GitHub stars GitHub forks

This project is a Angular 4+ pipe to truncate text to a set of characters or words.

CI Status

CI Status

Installation

To install this library, run:

$ npm install @yellowspot/ng-truncate --save

Usage

<p>{{ "123456789" | truncate : 3 : '…' }}</p>

Options as a hash

<p>{{ "123456789" | truncate : 3 : { trailingString: '…', countTrailing: true }}</p>

Examples

By default, the pipe will truncate text after 40 characters. You could override this using the first argument:

import { Component } from '@angular/core';
import { TruncateModule } from '@yellowspot/ng-truncate';

@Component({
    selector: 'my-component',
    template: '<p>{{ "123456789" | truncate : 3 }}</p>'
})
export class MyComponent {

}

@NgModule({
  imports: [ TruncateModule ],
  declarations: [ MyComponent ]
})
export class MyApp { }

This will produce the following html

<p>123…</p>

There is a second argument which allow to override the suffix used:

@Component({
    ...
    template: '<p>{{ "123456789" | truncate : 3 : "xxx" }}</p>',
    ...
})

This will produce the following html

<p>123xxx</p>

You can also truncate left side by using negative limit

@Component({
    ...
    template: '<p>{{ "123456789" | truncate : -4 : "…" }}</p>',
    ...
})

This will produce the following html

<p>…6789</p>

Count trailing string as part of the limit

@Component({
    ...
    template: '<p>{{ "123456789" | truncate : 3 : { trailingString: "…", countTrailing: true } }}</p>',
    ...
})

This will produce the following html

<p>12…</p>

Truncate by words

Using TruncateModule also enable truncating by words

import { Component } from '@angular/core';
import { TruncateModule } from '@yellowspot/ng-truncate';

@Component({
    selector: 'my-component',
    template: '<p>{{ "1234 567 89" | words : 2 }}</p>'
})
export class MyComponent {

}

@NgModule({
  imports: [ TruncateModule ],
  declarations: [ MyComponent ]
})
export class MyApp { }

This will produce the following html

<p>1234 567…</p>

This pipe has also a second parameter to override the suffix used

Demo

Check out the Live demo

...Or modify on CodeSanbox here

...Or clone the demo app built using angular-cli: https://github.com/yellowspot/ng2-truncate-demo

Development

To generate all *.js, *.js.map and *.d.ts files:

$ npm run ng build @yellowspot/ng-truncate

To lint all *.ts files:

$ npm run ng lint @yellowspot/ng-truncate

To execute all test via via Karma:

$ npm run ng test @yellowspot/ng-truncate

Publish:

$ npm run build-lib
$ cd dist/yellowspot/ng-truncate/
$ npm publish

ng2-truncate's People

Contributors

anmuel avatar bojankogoj avatar cristianstu avatar dependabot-preview[bot] avatar dependabot[bot] avatar itsnotvalid avatar mgrunberg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

ng2-truncate's Issues

Jest error

After adding this module to my applications a got an error from jest:


         Jest encountered an unexpected token

         This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

         By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

         Here's what you can do:
          • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
          • If you need a custom transformation specify a "transform" option in your config.
          • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

         You'll find more details and examples of these config options in the docs:
         https://jestjs.io/docs/en/configuration.html

         Details:

         /home/tibi/git/standBystand/node_modules/@yellowspot/ng-truncate/dist/index.js:1
         ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export { TruncateModule, TRUNCATE_PIPES } from './truncate.module';
                                                                                                  ^^^^^^

         SyntaxError: Unexpected token export



           at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
           at Object.<anonymous> (src/main/webapp/app/shared/shared.module.ts:279:72)```

Problem with loading module

After upgrading to Angular 4.0 I have this error:
ERROR Error: Uncaught (in promise): Error: (SystemJS) Unexpected token <
SyntaxError: Unexpected token <
at eval ()
at ZoneDelegate.invoke (http://localhost:3000/assets/zone.js/dist/zone.js:366:26)
at Object.onInvoke (http://localhost:3000/assets/@angular/core/bundles/core.umd.js:4133:37)
at ZoneDelegate.invoke (http://localhost:3000/assets/zone.js/dist/zone.js:365:32)
at Zone.run (http://localhost:3000/assets/zone.js/dist/zone.js:126:43)
Evaluating http://localhost:3000/traceur
Error loading http://localhost:3000/traceur
Unable to load transpiler to transpile http://localhost:3000/assets/ng2-truncate/dist/index.js
Error loading http://localhost:3000/assets/ng2-truncate/dist/index.js as "ng2-truncate" from http://localhost:3000/assets/angular/base/organizer/organizer.module.js
(SystemJS) Unexpected token <
SyntaxError: Unexpected token <
at eval ()
at ZoneDelegate.invoke (http://localhost:3000/assets/zone.js/dist/zone.js:366:26)
at Object.onInvoke (http://localhost:3000/assets/@angular/core/bundles/core.umd.js:4133:37)
at ZoneDelegate.invoke (http://localhost:3000/assets/zone.js/dist/zone.js:365:32)
at Zone.run (http://localhost:3000/assets/zone.js/dist/zone.js:126:43)
Evaluating http://localhost:3000/traceur
Error loading http://localhost:3000/traceur
Unable to load transpiler to transpile http://localhost:3000/assets/ng2-truncate/dist/index.js
Error loading http://localhost:3000/assets/ng2-truncate/dist/index.js as "ng2-truncate" from http://localhost:3000/assets/angular/base/organizer/organizer.module.js
at eval ()
at ZoneDelegate.invoke (http://localhost:3000/assets/zone.js/dist/zone.js:366:26)
at Object.onInvoke (http://localhost:3000/assets/@angular/core/bundles/core.umd.js:4133:37)
at ZoneDelegate.invoke (http://localhost:3000/assets/zone.js/dist/zone.js:365:32)
at Zone.run (http://localhost:3000/assets/zone.js/dist/zone.js:126:43)
Evaluating http://localhost:3000/traceur
Error loading http://localhost:3000/traceur
Unable to load transpiler to transpile http://localhost:3000/assets/ng2-truncate/dist/index.js
Error loading http://localhost:3000/assets/ng2-truncate/dist/index.js as "ng2-truncate" from http://localhost:3000/assets/angular/base/organizer/organizer.module.js
(SystemJS) Unexpected token <
SyntaxError: Unexpected token <
at eval ()
at ZoneDelegate.invoke (http://localhost:3000/assets/zone.js/dist/zone.js:366:26)
at Object.onInvoke (http://localhost:3000/assets/@angular/core/bundles/core.umd.js:4133:37)
at ZoneDelegate.invoke (http://localhost:3000/assets/zone.js/dist/zone.js:365:32)
at Zone.run (http://localhost:3000/assets/zone.js/dist/zone.js:126:43)
Evaluating http://localhost:3000/traceur
Error loading http://localhost:3000/traceur
Unable to load transpiler to transpile http://localhost:3000/assets/ng2-truncate/dist/index.js
Error loading http://localhost:3000/assets/ng2-truncate/dist/index.js as "ng2-truncate" from http://localhost:3000/assets/angular/base/organizer/organizer.module.js
at resolvePromise (http://localhost:3000/assets/zone.js/dist/zone.js:713:31) [angular]
at resolvePromise (http://localhost:3000/assets/zone.js/dist/zone.js:684:17) [angular]
at http://localhost:3000/assets/zone.js/dist/zone.js:761:17 [angular]
at Object.onInvokeTask (http://localhost:3000/assets/@angular/core/bundles/core.umd.js:4124:37) [angular]
at ZoneDelegate.invokeTask (http://localhost:3000/assets/zone.js/dist/zone.js:398:36) [angular]
at Zone.runTask (http://localhost:3000/assets/zone.js/dist/zone.js:166:47) [ => angular]
at drainMicroTaskQueue (http://localhost:3000/assets/zone.js/dist/zone.js:594:35) []
at XMLHttpRequest.ZoneTask.invoke (http://localhost:3000/assets/zone.js/dist/zone.js:465:25) []

ng2-truncate is loaded with systemjs like this:
System.config({
map: {
......
'ng2-truncate': '/assets/ng2-truncate/dist',
.......
},
packages: {
.......
'ng2-truncate': {main: 'index', defaultExtension: 'js'},
.......
}
});
also tried
System.config({
map: {
......
'ng2-truncate': '/assets/ng2-truncate',
.......
},
packages: {
.......
'ng2-truncate': {main: 'dist/index', defaultExtension: 'js'},
.......
}
});
What is wrong?

NgModule

How about updating it to a release version using NgModule?

Missing license?

I was a bit unsure wether I can use this package in a project of mine, as the npmjs.com package page shows License "none".
Do I have something to consider? Can we add a LICENCE file / information to the package.json?

AOT Compilation failed in ng2-truncate/dist/index.d.ts version 1.2.0

C:\Users\budcr\Source\Workspaces\CloudForestWebSite\CloudForest\wwwroot>"node_modules/.bin/ngc" -p tsconfig-aot.json
Error: Error encountered resolving symbol values statically. Only initialized variables and constants can be referenced because the value of this variable is needed by the template compiler (position 2:22 in the original .ts file), resolving symbol TRUNCATE_PIPES in C:/Users/budcr/Source/Workspaces/CloudForestWebSite/CloudForest/wwwroot/node_modules/ng2-truncate/dist/index.d.ts, resolving symbol AppModule in C:/Users/budcr/Source/Workspaces/CloudForestWebSite/CloudForest/wwwroot/app/app.module.ts, resolving symbol AppModule in C:/Users/budcr/Source/Workspaces/CloudForestWebSite/CloudForest/wwwroot/app/app.module.ts
at positionalError (C:\Users\budcr\Source\Workspaces\CloudForestWebSite\CloudForest\wwwroot\node_modules@angular\compiler\bundles\compiler.umd.js:25894:37)
at simplifyInContext (C:\Users\budcr\Source\Workspaces\CloudForestWebSite\CloudForest\wwwroot\node_modules@angular\compiler\bundles\compiler.umd.js:25731:29)
at StaticReflector.simplify (C:\Users\budcr\Source\Workspaces\CloudForestWebSite\CloudForest\wwwroot\node_modules@angular\compiler\bundles\compiler.umd.js:25745:15)
at StaticReflector.annotations (C:\Users\budcr\Source\Workspaces\CloudForestWebSite\CloudForest\wwwroot\node_modules@angular\compiler\bundles\compiler.umd.js:25243:62)
at NgModuleResolver.resolve (C:\Users\budcr\Source\Workspaces\CloudForestWebSite\CloudForest\wwwroot\node_modules@angular\compiler\bundles\compiler.umd.js:17584:84)
at CompileMetadataResolver.getNgModuleMetadata (C:\Users\budcr\Source\Workspaces\CloudForestWebSite\CloudForest\wwwroot\node_modules@angular\compiler\bundles\compiler.umd.js:18092:62)
at addNgModule (C:\Users\budcr\Source\Workspaces\CloudForestWebSite\CloudForest\wwwroot\node_modules@angular\compiler\bundles\compiler.umd.js:25005:60)
at C:\Users\budcr\Source\Workspaces\CloudForestWebSite\CloudForest\wwwroot\node_modules@angular\compiler\bundles\compiler.umd.js:25016:16
at Array.forEach (native)
at _createNgModules (C:\Users\budcr\Source\Workspaces\CloudForestWebSite\CloudForest\wwwroot\node_modules@angular\compiler\bundles\compiler.umd.js:25015:28)
Compilation failed

Unmet Peer dependency [email protected]

cli packages: (C:\Users\lokuge\AppData\Roaming\npm\node_modules)

    @ionic/cli-utils  : 1.16.0
    ionic (Ionic CLI) : 3.16.0

global packages:

    cordova (Cordova CLI) : 7.0.1

local packages:

    @ionic/app-scripts : 3.0.1
    Cordova Platforms  : android 6.2.3
    Ionic Framework    : ionic-angular 3.8.0

System:

    Android SDK Tools : 25.2.5
    Node              : v6.10.0
    npm               : 3.10.10
    OS                : Windows 8.1

Environment Variables:

    ANDROID_HOME : C:\Program Files (x86)\Android\android-sdk

Misc:

    backend : pro

package.json

{
    "name": "Tracker",
    "version": "0.0.1",
    "author": "Ionic Framework",
    "homepage": "http://ionicframework.com/",
    "private": true,
    "scripts": {
        "clean": "ionic-app-scripts clean",
        "build": "ionic-app-scripts build",
        "lint": "ionic-app-scripts lint",
        "ionic:build": "ionic-app-scripts build",
        "ionic:serve": "ionic-app-scripts serve"
    },
    "dependencies": {
        "@angular/common": "4.4.4",
        "@angular/compiler": "4.4.4",
        "@angular/compiler-cli": "4.4.4",
        "@angular/core": "4.4.4",
        "@angular/forms": "4.4.4",
        "@angular/http": "4.4.4",
        "@angular/platform-browser": "4.4.4",
        "@angular/platform-browser-dynamic": "4.4.4",
        "@ionic-native/camera": "^4.3.2",
        "@ionic-native/core": "4.3.2",
        "@ionic-native/splash-screen": "4.3.2",
        "@ionic-native/status-bar": "4.3.2",
        "@ionic/storage": "2.0.1",
        "angular2-text-mask": "8.0.2",
        "cordova-android": "^6.2.3",
        "cordova-plugin-camera": "^2.4.1",
        "cordova-plugin-compat": "^1.1.0",
        "cordova-plugin-console": "^1.0.5",
        "cordova-plugin-device": "^1.1.4",
        "cordova-plugin-ionic-webview": "^1.1.16",
        "cordova-plugin-splashscreen": "^4.0.3",
        "cordova-plugin-statusbar": "^2.2.2",
        "cordova-plugin-whitelist": "^1.3.1",
        "cordova-sqlite-storage": "^2.0.4",
        "highcharts": "^6.0.2",
        "highcharts-more": "^0.1.2",
        "ionic-angular": "3.8.0",
        "ionic-img-viewer": "^2.7.3",
        "ionic-plugin-keyboard": "^2.2.1",
        "ionicons": "3.0.0",
        "moment": "^2.18.1",
        "ng2-truncate": "^1.3.11",
        "rxjs": "5.4.3",
        "sw-toolbox": "3.6.0",
        "text-mask-addons": "^3.6.0",
        "zone.js": "0.8.18"
    },
    "devDependencies": {
        "@ionic/app-scripts": "3.0.1",
        "typescript": "2.3.4"
    },
    "description": "An Ionic project",
    "cordova": {
        "plugins": {
            "cordova-plugin-console": {},
            "cordova-plugin-device": {},
            "cordova-plugin-splashscreen": {},
            "cordova-plugin-statusbar": {},
            "cordova-plugin-whitelist": {},
            "ionic-plugin-keyboard": {},
            "cordova-sqlite-storage": {},
            "cordova-plugin-ionic-webview": {},
            "cordova-plugin-camera": {}
        },
        "platforms": [
            "android"
        ]
    }
}

Fix specs

Specs are not running on last version.

Calling function 'ɵmakeDecorator', function calls are not supported

This is an error when i try to build a ios --prod with ionic.

This is the error:

Error: Error encountered resolving symbol values statically. Calling function 'ɵmakeDecorator', function calls are not supported. Consider replacing the function or lambda with a reference to an exported function, resolving symbol Injectable in /Users/filippodicostanzo/Desktop/IonicProjects/CinemaIris2.1/node_modules/ng2-truncate/node_modules/@angular/core/core.d.ts, resolving symbol ɵe in /Users/filippodicostanzo/Desktop/IonicProjects/CinemaIris2.1/node_modules/ng2-truncate/node_modules/@angular/core/core.d.ts, resolving symbol ɵe in /Users/filippodicostanzo/Desktop/IonicProjects/CinemaIris2.1/node_modules/ng2-truncate/node_modules/@angular/core/core.d.ts at Error (native) at syntaxError (/Users/filippodicostanzo/Desktop/IonicProjects/CinemaIris2.1/node_modules/@angular/compiler/bundles/compiler.umd.js:1550:34) at simplifyInContext (/Users/filippodicostanzo/Desktop/IonicProjects/CinemaIris2.1/node_modules/@angular/compiler/bundles/compiler.umd.js:23870:23) at StaticReflector.simplify (/Users/filippodicostanzo/Desktop/IonicProjects/CinemaIris2.1/node_modules/@angular/compiler/bundles/compiler.umd.js:23882:13) at StaticReflector.annotations (/Users/filippodicostanzo/Desktop/IonicProjects/CinemaIris2.1/node_modules/@angular/compiler/bundles/compiler.umd.js:23331:41) at NgModuleResolver.resolve (/Users/filippodicostanzo/Desktop/IonicProjects/CinemaIris2.1/node_modules/@angular/compiler/bundles/compiler.umd.js:13883:70) at CompileMetadataResolver.getNgModuleMetadata (/Users/filippodicostanzo/Desktop/IonicProjects/CinemaIris2.1/node_modules/@angular/compiler/bundles/compiler.umd.js:14473:60) at addNgModule (/Users/filippodicostanzo/Desktop/IonicProjects/CinemaIris2.1/node_modules/@angular/compiler/bundles/compiler.umd.js:23050:58) at /Users/filippodicostanzo/Desktop/IonicProjects/CinemaIris2.1/node_modules/@angular/compiler/bundles/compiler.umd.js:23061:14 at Array.forEach (native)

Any solution?

No issue with 1.3.7 version!

Truncate HTML content

Is there any way to truncate HTML content?
I have content generated with TinyMCE and it contains generated elements and styles, like:

<p style="margin: 0px 0px 15px; padding: 0px; text-align: justify; font-family: ''Open Sans'', Arial, sans-serif;">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer pellentesque odio nec sollicitudin accumsan. Donec congue porta metus ut tempus. In in tristique libero. Praesent quam eros, aliquet ac felis sed, eleifend mollis dolor. Aenean tempor finibus massa, sit amet vehicula ipsum facilisis eget. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aliquam facilisis mauris laoreet felis sodales sodales. Sed imperdiet fringilla libero, non euismod tortor elementum eget. In purus erat, imperdiet eu lacus eu, consectetur cursus dui.</p>

So if I set "words : 5" for example, it will return an empty string.

There is a problem while I am trying to build my app, with your package, using the angular 8 ivy engine

I am trying to build my app but the angular compiler complains about on build time that your dependency does not have a proper format.

Please look at this.

Error message:

× The app1 build failed

ERROR in There is no format with import statements in 'C:/dev/apps/app1/apps/node_modules/ng2-truncate' entry-point.
error while building all features:
ERROR in There is no format with import statements in 'C:/dev/apps/app1/apps/node_modules/ng2-truncate' entry-point.

tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "importHelpers": true, 
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "module": "es2015",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ],
    "baseUrl": "./",
    "paths": {
      "@app-global/*":              ["src/app/*"],
    }
  },
  "angularCompilerOptions": {
    "enableIvy": true <--------------
  }
}

Cannot run tests: Unexpected token export

  ● Test suite failed to run

    /home/niko/WebstormProjects/digital_services/WebApp/packages/client/node_modules/@yellowspot/ng-truncate/dist/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export { TruncateModule, TRUNCATE_PIPES } from './truncate.module';
                                                                                             ^^^^^^
    
    SyntaxError: Unexpected token export

      17 | import { TruncateModule } from '@yellowspot/ng-truncate';

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.