Socket
Socket
Sign inDemoInstall

article-json-selection-utils

Package Overview
Dependencies
3
Maintainers
3
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.2.0 to 2.0.0

4

dist/get-selection-text.js

@@ -14,4 +14,6 @@ 'use strict';

exports.default = function (articleJson) {
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var result = [];
(0, _mapSelectionText2.default)(articleJson, function (text) {
(0, _mapSelectionText2.default)(articleJson, options, function (text) {
return result.push(text);

@@ -18,0 +20,0 @@ });

@@ -11,10 +11,23 @@ 'use strict';

exports.default = function (articleJson, fn) {
exports.default = function (articleJson, options, fn) {
if (typeof options === 'function') {
fn = options;
options = {};
}
var _options = options;
var _options$includeBound = _options.includeBoundary;
var includeBoundary = _options$includeBound === undefined ? false : _options$includeBound;
var inSelection = false;
var mapSelected = function mapSelected(child) {
if (child.mark && child.markClass === 'selection-start') {
var selectionStart = child.mark && child.markClass === 'selection-start';
var selectionEnd = child.mark && child.markClass === 'selection-end';
if (selectionStart) {
inSelection = true;
}
if (child.mark && child.markClass === 'selection-end') {
if (selectionEnd) {
inSelection = false;

@@ -27,3 +40,5 @@ }

return inSelection && child.content ? fn(child) : child;
var shouldMap = inSelection && !selectionStart || includeBoundary && (selectionStart || selectionEnd) && child.type === 'text';
return shouldMap ? fn(child) : child;
};

@@ -30,0 +45,0 @@

import mapSelectionText from './map-selection-text';
export default articleJson => {
export default (articleJson, options = {}) => {
const result = [];
mapSelectionText(articleJson, text => result.push(text));
mapSelectionText(articleJson, options, text => result.push(text));
return result;
};
import {map} from 'immutable-array-methods';
import {set} from 'immutable-object-methods';
export default (articleJson, fn) => {
export default (articleJson, options, fn) => {
if (typeof options === 'function') {
fn = options;
options = {};
}
const {includeBoundary = false} = options;
let inSelection = false;
const mapSelected = (child) => {
if (child.mark && child.markClass === 'selection-start') {
const selectionStart = child.mark && child.markClass === 'selection-start';
const selectionEnd = child.mark && child.markClass === 'selection-end';
if (selectionStart) {
inSelection = true;
}
if (child.mark && child.markClass === 'selection-end') {
if (selectionEnd) {
inSelection = false;

@@ -19,3 +29,7 @@ }

return inSelection && child.content ? fn(child) : child;
const shouldMap = inSelection && !selectionStart ||
(includeBoundary && (selectionStart || selectionEnd)) &&
child.type === 'text';
return shouldMap ? fn(child) : child;
};

@@ -22,0 +36,0 @@

{
"name": "article-json-selection-utils",
"version": "1.2.0",
"version": "2.0.0",
"description": "Get, return & modify article-json selections",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

@@ -13,3 +13,3 @@ import test from 'tapava';

type: 'paragraph', children: [{
content: 'hello, world!'
type: 'text', content: 'hello, world!'
}]

@@ -25,9 +25,9 @@ }]);

children: [
{mark: true, markClass: 'selection-start'},
{content: 'hello, world!'},
{mark: true, markClass: 'selection-end'}
{type: 'text', mark: true, markClass: 'selection-start'},
{type: 'text', content: 'hello, world!'},
{type: 'text', mark: true, markClass: 'selection-end'}
]
}];
const actual = getSelectionText(articleJson);
const expected = [{content: 'hello, world!'}];
const expected = [{type: 'text', content: 'hello, world!'}];
t.deepEqual(actual, expected);

@@ -49,3 +49,3 @@ });

type: 'paragraph',
children: [{content: 'beep boop'}]
children: [{type: 'text', content: 'beep boop'}]
}, {

@@ -56,5 +56,5 @@ type: 'blockquote',

children: [
{mark: true, markClass: 'selection-start'},
{content: 'hello, world!'},
{mark: true, markClass: 'selection-end'}
{type: 'text', mark: true, markClass: 'selection-start'},
{type: 'text', content: 'hello, world!'},
{type: 'text', mark: true, markClass: 'selection-end'}
]}

@@ -64,3 +64,3 @@ ]

type: 'paragraph',
children: [{content: 'foo bar'}]
children: [{type: 'text', content: 'foo bar'}]
}

@@ -70,4 +70,22 @@ ];

const actual = getSelectionText(articleJson);
const expected = [{content: 'hello, world!'}];
const expected = [{type: 'text', content: 'hello, world!'}];
t.deepEqual(actual, expected);
});
test('getSelectionText() includeBoundary=true', t => {
const articleJson = [{
type: 'paragraph',
children: [
{type: 'text', mark: true, markClass: 'selection-start'},
{type: 'text', content: 'hello, world!'},
{type: 'text', mark: true, markClass: 'selection-end'}
]
}];
const actual = getSelectionText(articleJson, {includeBoundary: true});
const expected = [
{type: 'text', mark: true, markClass: 'selection-start'},
{type: 'text', content: 'hello, world!'},
{type: 'text', mark: true, markClass: 'selection-end'}
];
t.deepEqual(actual, expected);
});
import test from 'tapava';
import {mapSelectionText as _mapSelectionText} from '../lib';
import {mergeDeep} from 'immutable-object-methods';
const mapSelectionText = (articleJson, fn) => _mapSelectionText(Object.freeze(articleJson), fn);
const mapSelectionText = (articleJson, options, fn) => _mapSelectionText(Object.freeze(articleJson), options, fn);
const shouldNotBeCalled = () => {

@@ -19,3 +20,3 @@ throw new Error('this method should not be called');

type: 'paragraph', children: [{
content: 'hello, world!'
type: 'text', content: 'hello, world!'
}]

@@ -32,14 +33,14 @@ }];

children: [
{mark: true, markClass: 'selection-start'},
{content: 'hello, world!'},
{mark: true, markClass: 'selection-end'}
{type: 'text', mark: true, markClass: 'selection-start'},
{type: 'text', content: 'hello, world!'},
{type: 'text', mark: true, markClass: 'selection-end'}
]
}];
const actual = mapSelectionText(articleJson, ({content}) => ({italic: true, content}));
const actual = mapSelectionText(articleJson, (item) => (mergeDeep(item, {italic: true})));
const expected = [{
type: 'paragraph',
children: [
{mark: true, markClass: 'selection-start'},
{content: 'hello, world!', italic: true},
{mark: true, markClass: 'selection-end'}
{type: 'text', mark: true, markClass: 'selection-start'},
{type: 'text', content: 'hello, world!', italic: true},
{type: 'text', mark: true, markClass: 'selection-end'}
]

@@ -64,5 +65,5 @@ }];

children: [
{content: 'before'},
{mark: true, markClass: 'selection-start'},
{content: 'hello'}
{type: 'text', content: 'before'},
{type: 'text', mark: true, markClass: 'selection-start'},
{type: 'text', content: 'hello'}
]

@@ -75,14 +76,14 @@ }, {

children: [
{content: 'world!'},
{mark: true, markClass: 'selection-end'},
{content: 'after'}
{type: 'text', content: 'world!'},
{type: 'text', mark: true, markClass: 'selection-end'},
{type: 'text', content: 'after'}
]
}];
const actual = mapSelectionText(articleJson, ({content}) => ({italic: true, content}));
const actual = mapSelectionText(articleJson, (item) => (mergeDeep(item, {italic: true})));
const expected = [{
type: 'paragraph',
children: [
{content: 'before'},
{mark: true, markClass: 'selection-start'},
{content: 'hello', italic: true}
{type: 'text', content: 'before'},
{type: 'text', mark: true, markClass: 'selection-start'},
{type: 'text', content: 'hello', italic: true}
]

@@ -95,5 +96,5 @@ }, {

children: [
{content: 'world!', italic: true},
{mark: true, markClass: 'selection-end'},
{content: 'after'}
{type: 'text', content: 'world!', italic: true},
{type: 'text', mark: true, markClass: 'selection-end'},
{type: 'text', content: 'after'}
]

@@ -108,3 +109,3 @@ }];

type: 'paragraph',
children: [{content: 'beep boop'}]
children: [{type: 'text', content: 'beep boop'}]
}, {

@@ -115,6 +116,6 @@ type: 'blockquote',

children: [
{mark: true, markClass: 'selection-start'},
{content: 'hello, '},
{mark: true, markClass: 'selection-end'},
{content: 'world!'}
{type: 'text', mark: true, markClass: 'selection-start'},
{type: 'text', content: 'hello, '},
{type: 'text', mark: true, markClass: 'selection-end'},
{type: 'text', content: 'world!'}
]}

@@ -124,11 +125,11 @@ ]

type: 'paragraph',
children: [{content: 'foo bar'}]
children: [{type: 'text', content: 'foo bar'}]
}
];
const actual = mapSelectionText(articleJson, ({content}) => ({italic: true, content}));
const actual = mapSelectionText(articleJson, (item) => (mergeDeep(item, {italic: true})));
const expected = [
{
type: 'paragraph',
children: [{content: 'beep boop'}]
children: [{type: 'text', content: 'beep boop'}]
}, {

@@ -139,6 +140,6 @@ type: 'blockquote',

children: [
{mark: true, markClass: 'selection-start'},
{content: 'hello, ', italic: true},
{mark: true, markClass: 'selection-end'},
{content: 'world!'}
{type: 'text', mark: true, markClass: 'selection-start'},
{type: 'text', content: 'hello, ', italic: true},
{type: 'text', mark: true, markClass: 'selection-end'},
{type: 'text', content: 'world!'}
]}

@@ -148,3 +149,3 @@ ]

type: 'paragraph',
children: [{content: 'foo bar'}]
children: [{type: 'text', content: 'foo bar'}]
}

@@ -154,1 +155,22 @@ ];

});
test('mapSelectionText() includeBoundary=true', t => {
const articleJson = [{
type: 'paragraph',
children: [
{type: 'text', mark: true, markClass: 'selection-start'},
{type: 'text', content: 'hello, world!'},
{type: 'text', mark: true, markClass: 'selection-end'}
]
}];
const actual = mapSelectionText(articleJson, {includeBoundary: true}, (item) => (mergeDeep(item, {italic: true})));
const expected = [{
type: 'paragraph',
children: [
{type: 'text', mark: true, markClass: 'selection-start', italic: true},
{type: 'text', content: 'hello, world!', italic: true},
{type: 'text', mark: true, markClass: 'selection-end', italic: true}
]
}];
t.deepEqual(actual, expected);
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc